summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/dialects
diff options
context:
space:
mode:
authorMike Bayer <classic@zzzcomputing.com>2013-06-08 14:39:17 -0400
committerMike Bayer <classic@zzzcomputing.com>2013-06-08 14:39:17 -0400
commit8bf1d3fd4e561c7a1dae2a76ca91fcf948889bde (patch)
treea9908dd9af7db38f429c3db168896561cf43b467 /lib/sqlalchemy/dialects
parenta341e1ccc9b4d12906f8cb423c45ac2f6f754891 (diff)
parent4cc9d482ae8edc2ac60aaf4102bab84148691fed (diff)
downloadsqlalchemy-8bf1d3fd4e561c7a1dae2a76ca91fcf948889bde.tar.gz
Merged in malor/sqlalchemy (pull request #1)
Fix using of 'mysql_length' for composite indexes
Diffstat (limited to 'lib/sqlalchemy/dialects')
-rw-r--r--lib/sqlalchemy/dialects/mysql/base.py32
1 files changed, 25 insertions, 7 deletions
diff --git a/lib/sqlalchemy/dialects/mysql/base.py b/lib/sqlalchemy/dialects/mysql/base.py
index 03827edb7..b7cff43eb 100644
--- a/lib/sqlalchemy/dialects/mysql/base.py
+++ b/lib/sqlalchemy/dialects/mysql/base.py
@@ -226,12 +226,15 @@ become part of the index. SQLAlchemy provides this feature via the
``mysql_length`` parameter::
Index('my_index', my_table.c.data, mysql_length=10)
+ Index('a_b_idx', my_table.c.a, my_table.c.b, mysql_length={'a': 4, 'b': 9})
Prefix lengths are given in characters for nonbinary string types and in bytes
-for binary string types. The value passed to the keyword argument will be
-simply passed through to the underlying CREATE INDEX command, so it *must* be
-an integer. MySQL only allows a length for an index if it is for a CHAR,
-VARCHAR, TEXT, BINARY, VARBINARY and BLOB.
+for binary string types. The value passed to the keyword argument *must* be
+either an integer (and, thus, specify the same prefix length value for all
+columns of the index) or a dict in which keys are column names and values are
+prefix length values for corresponding columns. MySQL only allows a length for
+a column of an index if it is for a CHAR, VARCHAR, TEXT, BINARY, VARBINARY and
+BLOB.
Index Types
~~~~~~~~~~~~~
@@ -1519,12 +1522,27 @@ class MySQLDDLCompiler(compiler.DDLCompiler):
text += "UNIQUE "
text += "INDEX %s ON %s " % (name, table)
- columns = ', '.join(columns)
if 'mysql_length' in index.kwargs:
length = index.kwargs['mysql_length']
- text += "(%s(%d))" % (columns, length)
+
+ # length value can be an integer value specifying the same
+ # prefix length for all columns of the index
+ try:
+ columns = ', '.join(
+ '%s(%d)' % (col, length)
+ for col in columns
+ )
+ # otherwise it's a (column_name --> integer value) mapping
+ # specifying the prefix length for each column of the index
+ except TypeError:
+ columns = ', '.join(
+ ('%s(%d)' % (col, length[col])
+ if col in length else '%s' % col)
+ for col in columns
+ )
else:
- text += "(%s)" % (columns)
+ columns = ', '.join(columns)
+ text += '(%s)' % columns
if 'mysql_using' in index.kwargs:
using = index.kwargs['mysql_using']