diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2014-06-18 10:56:23 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2014-06-18 10:56:57 -0400 |
commit | 6f9a8034ca20c5bb92b58068c279e0300f3dda10 (patch) | |
tree | f994818033ee46b52418d39c09ccc7798e777c90 | |
parent | 01eb52b516f19bb9b8aef4b8657a27c14063aad5 (diff) | |
download | sqlalchemy-6f9a8034ca20c5bb92b58068c279e0300f3dda10.tar.gz |
- 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
-rw-r--r-- | doc/build/changelog/changelog_08.rst | 11 | ||||
-rw-r--r-- | lib/sqlalchemy/dialects/mysql/base.py | 10 | ||||
-rw-r--r-- | test/dialect/mysql/test_compiler.py | 33 |
3 files changed, 51 insertions, 3 deletions
diff --git a/doc/build/changelog/changelog_08.rst b/doc/build/changelog/changelog_08.rst index 7736f7799..51aba11fe 100644 --- a/doc/build/changelog/changelog_08.rst +++ b/doc/build/changelog/changelog_08.rst @@ -12,6 +12,17 @@ :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 :tickets: 3062 diff --git a/lib/sqlalchemy/dialects/mysql/base.py b/lib/sqlalchemy/dialects/mysql/base.py index 91a860878..a0a97ba81 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 d479e2148..c8684ac58 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, |