]> 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:23 +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 6978162c22e2bc7675d17cd3b878417696513b63..b754f20212dc3bfd3e40942a46a16137a9af39e2 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 40715cc317755737bcde498e316d63a8b8379539..6d8b6a07c87a1ee6cae79d523d8a80785cc1b8f1 100644 (file)
@@ -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
index 22490c02a930347e9ac1cae7e6dceb8711cbf959..3c2782994dbe1a696538257f5f8787d83a45f4bb 100644 (file)
@@ -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,