Skip to content

Commit 7c513bb

Browse files
Issue #90 - fix alter nullability for foreign key (#93)
* Issue #90 bug fix
1 parent 5c2fa77 commit 7c513bb

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

mssql/schema.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -439,21 +439,24 @@ def _alter_field(self, model, old_field, new_field, old_type, new_type,
439439
fragment = self._alter_column_null_sql(model, old_field, new_field)
440440
if fragment:
441441
null_actions.append(fragment)
442-
if not new_field.null:
443-
# Drop unique constraint, SQL Server requires explicit deletion
444-
self._delete_unique_constraints(model, old_field, new_field, strict)
445-
# Drop indexes, SQL Server requires explicit deletion
446-
indexes_dropped = self._delete_indexes(model, old_field, new_field)
447-
if (
448-
new_field.get_internal_type() not in ("JSONField", "TextField") and
449-
(old_field.db_index or not new_field.db_index) and
450-
new_field.db_index or
451-
(indexes_dropped and sorted(indexes_dropped) == sorted(
452-
[index.name for index in model._meta.indexes]))
453-
):
454-
create_index_sql_statement = self._create_index_sql(model, [new_field])
455-
if create_index_sql_statement.__str__() not in [sql.__str__() for sql in self.deferred_sql]:
456-
post_actions.append((create_index_sql_statement, ()))
442+
# Drop unique constraint, SQL Server requires explicit deletion
443+
self._delete_unique_constraints(model, old_field, new_field, strict)
444+
# Drop indexes, SQL Server requires explicit deletion
445+
indexes_dropped = self._delete_indexes(model, old_field, new_field)
446+
auto_index_names = []
447+
for index_from_meta in model._meta.indexes:
448+
auto_index_names.append(self._create_index_name(model._meta.db_table, index_from_meta.fields))
449+
450+
if (
451+
new_field.get_internal_type() not in ("JSONField", "TextField") and
452+
(old_field.db_index or not new_field.db_index) and
453+
new_field.db_index or
454+
((indexes_dropped and sorted(indexes_dropped) == sorted([index.name for index in model._meta.indexes])) or
455+
(indexes_dropped and sorted(indexes_dropped) == sorted(auto_index_names)))
456+
):
457+
create_index_sql_statement = self._create_index_sql(model, [new_field])
458+
if create_index_sql_statement.__str__() not in [sql.__str__() for sql in self.deferred_sql]:
459+
post_actions.append((create_index_sql_statement, ()))
457460
# Only if we have a default and there is a change from NULL to NOT NULL
458461
four_way_default_alteration = (
459462
new_field.has_default() and
@@ -531,7 +534,7 @@ def _alter_field(self, model, old_field, new_field, old_type, new_type,
531534
self.execute(self._create_index_sql(model, [new_field]))
532535

533536
# Restore indexes & unique constraints deleted above, SQL Server requires explicit restoration
534-
if (old_type != new_type or (old_field.null and not new_field.null)) and (
537+
if (old_type != new_type or (old_field.null != new_field.null)) and (
535538
old_field.column == new_field.column
536539
):
537540
# Restore unique constraints

0 commit comments

Comments
 (0)