Skip to content
This repository
Browse code

Allow extra indexes to be specified, including multi-column &

unique
  • Loading branch information...
commit 1b6a505ea43237de3ba93f80cb683b69658a2b5e 1 parent 06e8cb9
Charles Leifer authored August 02, 2012

Showing 1 changed file with 10 additions and 4 deletions. Show diff stats Hide diff stats

  1. 14  peewee.py
14  peewee.py
@@ -2479,6 +2479,7 @@ def class_prepared(self):
2479 2479
 
2480 2480
 
2481 2481
 class BaseModelOptions(object):
  2482
+    indexes = None
2482 2483
     ordering = None
2483 2484
     pk_sequence = None
2484 2485
 
@@ -2551,7 +2552,7 @@ def rel_exists(self, model):
2551 2552
 
2552 2553
 
2553 2554
 class BaseModel(type):
2554  
-    inheritable_options = ['database', 'ordering', 'pk_sequence']
  2555
+    inheritable_options = ['database', 'indexes', 'ordering', 'pk_sequence']
2555 2556
 
2556 2557
     def __new__(cls, name, bases, attrs):
2557 2558
         cls = super(BaseModel, cls).__new__(cls, name, bases, attrs)
@@ -2684,13 +2685,18 @@ def create_table(cls, fail_silently=False, extra=''):
2684 2685
         if fail_silently and cls.table_exists():
2685 2686
             return
2686 2687
 
2687  
-        cls._meta.database.create_table(cls, extra=extra)
  2688
+        db = cls._meta.database
  2689
+        db.create_table(cls, extra=extra)
2688 2690
 
2689 2691
         for field_name, field_obj in cls._meta.fields.items():
2690 2692
             if isinstance(field_obj, ForeignKeyField):
2691  
-                cls._meta.database.create_foreign_key(cls, field_obj)
  2693
+                db.create_foreign_key(cls, field_obj)
2692 2694
             elif field_obj.db_index or field_obj.unique:
2693  
-                cls._meta.database.create_index(cls, field_obj.name, field_obj.unique)
  2695
+                db.create_index(cls, field_obj.name, field_obj.unique)
  2696
+
  2697
+        if cls._meta.indexes:
  2698
+            for fields, unique in cls._meta.indexes:
  2699
+                db.create_index(cls, fields, unique)
2694 2700
 
2695 2701
     @classmethod
2696 2702
     def drop_table(cls, fail_silently=False):

0 notes on commit 1b6a505

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