Skip to content

Commit 85bd6bf

Browse files
Jacob Cookpeakwinter
authored andcommitted
Additional fixes for unique nullable constraints
1 parent a39bfe1 commit 85bd6bf

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

sql_server/pyodbc/schema.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,11 @@ def _alter_field(self, model, old_field, new_field, old_type, new_type,
204204
# is to look at its name (refs #28053).
205205
continue
206206
self.execute(self._delete_constraint_sql(self.sql_delete_index, model, index_name))
207+
# Drop any unique nullable index/constraints, we'll remake them later if need be
208+
if old_field.unique and old_field.null:
209+
index_names = self._constraint_names(model, [old_field.column], unique=True, index=True)
210+
for index_name in index_names:
211+
self.execute(self._delete_constraint_sql(self.sql_delete_index, model, index_name))
207212
# Change check constraints?
208213
if old_db_params['check'] != new_db_params['check'] and old_db_params['check']:
209214
constraint_names = self._constraint_names(model, [old_field.column], check=True)
@@ -309,12 +314,12 @@ def _alter_field(self, model, old_field, new_field, old_type, new_type,
309314
if post_actions:
310315
for sql, params in post_actions:
311316
self.execute(sql, params)
312-
if not old_field.unique and new_field.unique:
317+
if new_field.unique:
313318
if new_field.null:
314319
self.execute(
315320
self._create_index_sql(model, [new_field], sql=self.sql_create_unique_null, suffix="_uniq")
316321
)
317-
else:
322+
elif not old_field.unique:
318323
self.execute(self._create_unique_sql(model, [new_field.column]))
319324
# Added an index?
320325
# constraint will no longer be used in lieu of an index. The following
@@ -723,7 +728,7 @@ def remove_field(self, model, field):
723728
})
724729
# Drop unique constraints, SQL Server requires explicit deletion
725730
for name, infodict in constraints.items():
726-
if field.column in infodict['columns'] and infodict['unique'] and not infodict['primary_key']:
731+
if field.column in infodict['columns'] and infodict['unique'] and not infodict['primary_key'] and not infodict['index']:
727732
self.execute(self.sql_delete_unique % {
728733
"table": self.quote_name(model._meta.db_table),
729734
"name": self.quote_name(name),

0 commit comments

Comments
 (0)