Skip to content

Commit 33389b6

Browse files
authored
Accept deferrable kwarg for schema editor SQL (#86)
This follows Django 3.1.x's lead in adding `deferrable` as a keyword argument to the `_create_unique_sql` function, following their implementation in the schema editor object. Without accepting this keyword arg, migrations will crash like so: ``` ... File "lib/python3.8/site-packages/django/db/backends/base/schema.py", line 360, in add_constraint sql = constraint.create_sql(model, self) File "lib/python3.8/site-packages/django/db/models/constraints.py", line 118, in create_sql return schema_editor._create_unique_sql( TypeError: _create_unique_sql() got an unexpected keyword argument 'deferrable' ``` This also adjusts the implementation to call `self_deferrable_constraint_sql(...)` with the deferrable arg; in this backend's case the return value is an empty string (like it is currently hardcoded). Essentially it's the same result but allows flexibility if this backend ever supported deferrable constraints. Backwards compatibility is maintained by checking for the passing of a deferrable keyword argument to the create sql function (not provided on earlier Django versions before 3.1) and that the database backend supports deferral for unique indexes.
1 parent d166fc9 commit 33389b6

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

sql_server/pyodbc/schema.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import binascii
22
import datetime
3+
import django
34

45
from django.db.backends.base.schema import (
56
BaseDatabaseSchemaEditor,
@@ -677,7 +678,10 @@ def add_field(self, model, field):
677678
if self.connection.features.connection_persists_old_columns:
678679
self.connection.close()
679680

680-
def _create_unique_sql(self, model, columns, name=None, condition=None):
681+
def _create_unique_sql(self, model, columns, name=None, condition=None, deferrable=None):
682+
if (deferrable and not getattr(self.connection.features, 'supports_deferrable_unique_constraints', False)):
683+
return None
684+
681685
def create_unique_name(*args, **kwargs):
682686
return self.quote_name(self._create_index_name(*args, **kwargs))
683687

@@ -687,22 +691,26 @@ def create_unique_name(*args, **kwargs):
687691
else:
688692
name = self.quote_name(name)
689693
columns = Columns(table, columns, self.quote_name)
694+
statement_args = {
695+
"deferrable": self._deferrable_constraint_sql(deferrable)
696+
} if django.VERSION >= (3, 1) else {}
697+
690698
if condition:
691699
return Statement(
692700
self.sql_create_unique_index,
693701
table=table,
694702
name=name,
695703
columns=columns,
696704
condition=' WHERE ' + condition,
697-
deferrable=''
705+
**statement_args
698706
) if self.connection.features.supports_partial_indexes else None
699707
else:
700708
return Statement(
701709
self.sql_create_unique,
702710
table=table,
703711
name=name,
704712
columns=columns,
705-
deferrable=''
713+
**statement_args
706714
)
707715

708716
def _create_index_sql(self, model, fields, *, name=None, suffix='', using='',

0 commit comments

Comments
 (0)