From: Mike Bayer Date: Wed, 18 Jun 2014 14:56:23 +0000 (-0400) Subject: - Fixed bug where column names added to ``mysql_length`` parameter X-Git-Tag: rel_0_9_5~9 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=4bab5e97b1f4080488caaf830a339c7e8474777a;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - Fixed bug where column names added to ``mysql_length`` parameter on an index needed to have the same quoting for quoted names in order to be recognized. The fix makes the quotes optional but also provides the old behavior for backwards compatibility with those using the workaround. fixes #3085 --- diff --git a/doc/build/changelog/changelog_08.rst b/doc/build/changelog/changelog_08.rst index 6978162c22..b754f20212 100644 --- a/doc/build/changelog/changelog_08.rst +++ b/doc/build/changelog/changelog_08.rst @@ -11,6 +11,17 @@ .. changelog:: :version: 0.8.7 + .. change:: + :tags: bug, mysql + :versions: 1.0.0, 0.9.5 + :tickets: 3085 + + Fixed bug where column names added to ``mysql_length`` parameter + on an index needed to have the same quoting for quoted names in + order to be recognized. The fix makes the quotes optional but + also provides the old behavior for backwards compatibility with those + using the workaround. + .. change:: :tags: bug, declarative :versions: 1.0.0, 0.9.5 diff --git a/lib/sqlalchemy/dialects/mysql/base.py b/lib/sqlalchemy/dialects/mysql/base.py index 85cbd6e223..2f78b428bd 100644 --- a/lib/sqlalchemy/dialects/mysql/base.py +++ b/lib/sqlalchemy/dialects/mysql/base.py @@ -1725,9 +1725,13 @@ class MySQLDDLCompiler(compiler.DDLCompiler): # length value can be a (column_name --> integer value) mapping # specifying the prefix length for each column of the index columns = ', '.join( - ('%s(%d)' % (col, length[col]) - if col in length else '%s' % col) - for col in columns + '%s(%d)' % (expr, length[col.name]) if col.name in length + else + ( + '%s(%d)' % (expr, length[expr]) if expr in length + else '%s' % expr + ) + for col, expr in zip(index.expressions, columns) ) else: # or can be an integer value specifying the same diff --git a/test/dialect/mysql/test_compiler.py b/test/dialect/mysql/test_compiler.py index 22490c02a9..3c2782994d 100644 --- a/test/dialect/mysql/test_compiler.py +++ b/test/dialect/mysql/test_compiler.py @@ -41,6 +41,39 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL): self.assert_compile(schema.CreateIndex(idx2), 'CREATE INDEX test_idx2 ON testtbl (data(5))') + def test_create_index_with_length_quoted(self): + m = MetaData() + tbl = Table('testtbl', m, Column('some quoted data', + String(255), key='s')) + idx1 = Index('test_idx1', tbl.c.s, mysql_length=10) + + self.assert_compile(schema.CreateIndex(idx1), + 'CREATE INDEX test_idx1 ON testtbl (`some quoted data`(10))') + + def test_create_composite_index_with_length_quoted(self): + m = MetaData() + tbl = Table('testtbl', m, + Column('some Quoted a', String(255), key='a'), + Column('some Quoted b', String(255), key='b')) + idx1 = Index('test_idx1', tbl.c.a, tbl.c.b, + mysql_length={'some Quoted a': 10, 'some Quoted b': 20}) + + self.assert_compile(schema.CreateIndex(idx1), + 'CREATE INDEX test_idx1 ON testtbl ' + '(`some Quoted a`(10), `some Quoted b`(20))') + + def test_create_composite_index_with_length_quoted_3085_workaround(self): + m = MetaData() + tbl = Table('testtbl', m, + Column('some quoted a', String(255), key='a'), + Column('some quoted b', String(255), key='b')) + idx1 = Index('test_idx1', tbl.c.a, tbl.c.b, + mysql_length={'`some quoted a`': 10, '`some quoted b`': 20}) + + self.assert_compile(schema.CreateIndex(idx1), + 'CREATE INDEX test_idx1 ON testtbl ' + '(`some quoted a`(10), `some quoted b`(20))') + def test_create_composite_index_with_length(self): m = MetaData() tbl = Table('testtbl', m,