]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- Fixed bug where column names added to ``mysql_length`` parameter
authorMike Bayer <mike_mp@zzzcomputing.com>
Wed, 18 Jun 2014 14:56:23 +0000 (10:56 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 18 Jun 2014 14:56:57 +0000 (10:56 -0400)
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

doc/build/changelog/changelog_08.rst
lib/sqlalchemy/dialects/mysql/base.py
test/dialect/mysql/test_compiler.py

index 7736f779963b47e6309a493685c8ac3540776c7d..51aba11fe8f3605896c47beedefb3aeabd7f24c0 100644 (file)
 .. 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
index 91a86087859a8bf2cb0f55f241ffd2d56df4a742..a0a97ba8144b4dc268701ba637a03456493d2b83 100644 (file)
@@ -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
index d479e21486ddf4beeb764722eff98d37397cf76d..c8684ac58e7a758e7d71fb570e9d35ee53d33683 100644 (file)
@@ -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,