diff options
| author | Ian Foote <python@ian.feete.org> | 2017-10-11 22:55:52 +0530 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2018-06-29 17:00:28 -0400 |
| commit | 38cada7c94f5f73d2d47a0a730ea5d71d266fa2c (patch) | |
| tree | f7cfb69df62a07b0cda4b3c0d1c4a2f6ea5780bf /django/db/models | |
| parent | b4cba4ed625ce7c88675616b3bbb237c28a926d1 (diff) | |
Fixed #28077 -- Added support for PostgreSQL opclasses in Index.
Thanks Vinay Karanam for the initial patch.
Diffstat (limited to 'django/db/models')
| -rw-r--r-- | django/db/models/indexes.py | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/django/db/models/indexes.py b/django/db/models/indexes.py index 9bfb9e0558..c378b13a5c 100644 --- a/django/db/models/indexes.py +++ b/django/db/models/indexes.py @@ -12,9 +12,15 @@ class Index: # cross-database compatibility with Oracle) max_name_length = 30 - def __init__(self, *, fields=(), name=None, db_tablespace=None): + def __init__(self, *, fields=(), name=None, db_tablespace=None, opclasses=()): + if opclasses and not name: + raise ValueError('An index must be named to use opclasses.') if not isinstance(fields, (list, tuple)): raise ValueError('Index.fields must be a list or tuple.') + if not isinstance(opclasses, (list, tuple)): + raise ValueError('Index.opclasses must be a list or tuple.') + if opclasses and len(fields) != len(opclasses): + raise ValueError('Index.fields and Index.opclasses must have the same number of elements.') if not fields: raise ValueError('At least one field is required to define an index.') self.fields = list(fields) @@ -31,6 +37,7 @@ class Index: if errors: raise ValueError(errors) self.db_tablespace = db_tablespace + self.opclasses = opclasses def check_name(self): errors = [] @@ -49,7 +56,7 @@ class Index: col_suffixes = [order[1] for order in self.fields_orders] return schema_editor._create_index_sql( model, fields, name=self.name, using=using, db_tablespace=self.db_tablespace, - col_suffixes=col_suffixes, + col_suffixes=col_suffixes, opclasses=self.opclasses, ) def remove_sql(self, model, schema_editor): @@ -65,6 +72,8 @@ class Index: kwargs = {'fields': self.fields, 'name': self.name} if self.db_tablespace is not None: kwargs['db_tablespace'] = self.db_tablespace + if self.opclasses: + kwargs['opclasses'] = self.opclasses return (path, (), kwargs) def clone(self): |
