Skip to content
This repository
Browse code

Support programmatic creation of multi-column indexes

  • Loading branch information...
commit 6b391d5fe55c07e22d33ec3d33146e60118726ba 1 parent 0f64480
Charles Leifer authored August 01, 2012

Showing 1 changed file with 16 additions and 11 deletions. Show diff stats Hide diff stats

  1. 27  peewee.py
27  peewee.py
@@ -409,18 +409,21 @@ def create_table_query(self, model_class, safe, extra='', framing=None):
409 409
     def create_table(self, model_class, safe=False, extra=''):
410 410
         self.execute(self.create_table_query(model_class, safe, extra))
411 411
 
412  
-    def create_index_query(self, model_class, field_name, unique, framing=None):
  412
+    def create_index_query(self, model_class, field_names, unique, framing=None):
413 413
         framing = framing or 'CREATE %(unique)s INDEX %(index)s ON %(table)s(%(field)s);'
414 414
 
415  
-        if field_name not in model_class._meta.fields:
416  
-            raise AttributeError(
417  
-                'Field %s not on model %s' % (field_name, model_class)
418  
-            )
419  
-
420  
-        field_obj = model_class._meta.fields[field_name]
  415
+        columns = []
  416
+        for field_name in field_names:
  417
+            if field_name not in model_class._meta.fields:
  418
+                raise AttributeError(
  419
+                    'Field %s not on model %s' % (field_name, model_class)
  420
+                )
  421
+            else:
  422
+                field_obj = model_class._meta.fields[field_name]
  423
+                columns.append(field_obj.db_column)
421 424
 
422 425
         db_table = model_class._meta.db_table
423  
-        index_name = self.quote_name('%s_%s' % (db_table, field_obj.db_column))
  426
+        index_name = self.quote_name('%s_%s' % (db_table, '_'.join(columns)))
424 427
 
425 428
         unique_expr = ternary(unique, 'UNIQUE', '')
426 429
 
@@ -428,11 +431,13 @@ def create_index_query(self, model_class, field_name, unique, framing=None):
428 431
             'unique': unique_expr,
429 432
             'index': index_name,
430 433
             'table': self.quote_name(db_table),
431  
-            'field': self.quote_name(field_obj.db_column),
  434
+            'field': ','.join(map(self.quote_name, columns)),
432 435
         }
433 436
 
434  
-    def create_index(self, model_class, field_name, unique=False):
435  
-        self.execute(self.create_index_query(model_class, field_name, unique))
  437
+    def create_index(self, model_class, field_names, unique=False):
  438
+        if isinstance(field_names, basestring):
  439
+            field_names = (field_names,)
  440
+        self.execute(self.create_index_query(model_class, field_names, unique))
436 441
 
437 442
     def create_foreign_key(self, model_class, field):
438 443
         return self.create_index(model_class, field.name, field.unique)

0 notes on commit 6b391d5

Please sign in to comment.
Something went wrong with that request. Please try again.