Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Support programmatic creation of multi-column indexes

  • Loading branch information...
commit 6b391d5fe55c07e22d33ec3d33146e60118726ba 1 parent 0f64480
@coleifer authored
Showing with 16 additions and 11 deletions.
  1. +16 −11 peewee.py
View
27 peewee.py
@@ -409,18 +409,21 @@ def create_table_query(self, model_class, safe, extra='', framing=None):
def create_table(self, model_class, safe=False, extra=''):
self.execute(self.create_table_query(model_class, safe, extra))
- def create_index_query(self, model_class, field_name, unique, framing=None):
+ def create_index_query(self, model_class, field_names, unique, framing=None):
framing = framing or 'CREATE %(unique)s INDEX %(index)s ON %(table)s(%(field)s);'
- if field_name not in model_class._meta.fields:
- raise AttributeError(
- 'Field %s not on model %s' % (field_name, model_class)
- )
-
- field_obj = model_class._meta.fields[field_name]
+ columns = []
+ for field_name in field_names:
+ if field_name not in model_class._meta.fields:
+ raise AttributeError(
+ 'Field %s not on model %s' % (field_name, model_class)
+ )
+ else:
+ field_obj = model_class._meta.fields[field_name]
+ columns.append(field_obj.db_column)
db_table = model_class._meta.db_table
- index_name = self.quote_name('%s_%s' % (db_table, field_obj.db_column))
+ index_name = self.quote_name('%s_%s' % (db_table, '_'.join(columns)))
unique_expr = ternary(unique, 'UNIQUE', '')
@@ -428,11 +431,13 @@ def create_index_query(self, model_class, field_name, unique, framing=None):
'unique': unique_expr,
'index': index_name,
'table': self.quote_name(db_table),
- 'field': self.quote_name(field_obj.db_column),
+ 'field': ','.join(map(self.quote_name, columns)),
}
- def create_index(self, model_class, field_name, unique=False):
- self.execute(self.create_index_query(model_class, field_name, unique))
+ def create_index(self, model_class, field_names, unique=False):
+ if isinstance(field_names, basestring):
+ field_names = (field_names,)
+ self.execute(self.create_index_query(model_class, field_names, unique))
def create_foreign_key(self, model_class, field):
return self.create_index(model_class, field.name, field.unique)
Please sign in to comment.
Something went wrong with that request. Please try again.