``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
a column of an index if it is for a CHAR, VARCHAR, TEXT, BINARY, VARBINARY and
BLOB.
+.. versionadded:: 0.8.2 ``mysql_length`` may now be specified as a dictionary
+ for use with composite indexes.
+
Index Types
~~~~~~~~~~~~~
if 'mysql_length' in index.kwargs:
length = index.kwargs['mysql_length']
- # length value can be an integer value specifying the same
- # prefix length for all columns of the index
- try:
+ if isinstance(length, dict):
+ # 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)
+ ('%s(%d)' % (col, length[col])
+ if col in length else '%s' % col)
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:
+ else:
+ # or can be an integer value specifying the same
+ # prefix length for all columns of the index
columns = ', '.join(
- ('%s(%d)' % (col, length[col])
- if col in length else '%s' % col)
+ '%s(%d)' % (col, length)
for col in columns
)
else: