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_8_7~14 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6f9a8034ca20c5bb92b58068c279e0300f3dda10;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 7736f77996..51aba11fe8 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 91a8608785..a0a97ba814 100644 --- a/lib/sqlalchemy/dialects/mysql/base.py +++ b/lib/sqlalchemy/dialects/mysql/base.py @@ -1673,9 +1673,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 d479e21486..c8684ac58e 100644 --- a/test/dialect/mysql/test_compiler.py +++ b/test/dialect/mysql/test_compiler.py @@ -40,6 +40,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,