Skip to content

Commit 6275ca6

Browse files
committed
Accept deferrable kwarg for schema editor SQL
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 6275ca6

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

sql_server/pyodbc/schema.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,16 @@ def add_field(self, model, field):
677677
if self.connection.features.connection_persists_old_columns:
678678
self.connection.close()
679679

680-
def _create_unique_sql(self, model, columns, name=None, condition=None):
680+
def _create_unique_sql(self, model, columns, name=None, condition=None, deferrable=None):
681+
statement_args = {}
682+
683+
if (deferrable):
684+
if (not getattr(self.connection.features, 'supports_deferrable_unique_constraints', False)
685+
):
686+
return None
687+
else:
688+
statement_args['deferrable'] = self._deferrable_constraint_sql(deferrable)
689+
681690
def create_unique_name(*args, **kwargs):
682691
return self.quote_name(self._create_index_name(*args, **kwargs))
683692

@@ -694,15 +703,15 @@ def create_unique_name(*args, **kwargs):
694703
name=name,
695704
columns=columns,
696705
condition=' WHERE ' + condition,
697-
deferrable=''
706+
**statement_args
698707
) if self.connection.features.supports_partial_indexes else None
699708
else:
700709
return Statement(
701710
self.sql_create_unique,
702711
table=table,
703712
name=name,
704713
columns=columns,
705-
deferrable=''
714+
**statement_args
706715
)
707716

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

0 commit comments

Comments
 (0)