Skip to content

Commit c42bd21

Browse files
committed
fix the issue michiya#52
1 parent 0ad0e03 commit c42bd21

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

sql_server/pyodbc/schema.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,22 @@ def _alter_field(self, model, old_field, new_field, old_type, new_type,
205205
},
206206
[],
207207
))
208+
# Drop unique constraint, SQL Server requires explicit deletion
209+
if old_field.unique and new_field.unique:
210+
constraint_names = self._constraint_names(model, [old_field.column], unique=True)
211+
if strict and len(constraint_names) != 1:
212+
raise ValueError("Found wrong number (%s) of unique constraints for %s.%s" % (
213+
len(constraint_names),
214+
model._meta.db_table,
215+
old_field.column,
216+
))
217+
for constraint_name in constraint_names:
218+
self.execute(self._delete_constraint_sql(self.sql_delete_unique, model, constraint_name))
219+
# Drop indexes, SQL Server requires explicit deletion
220+
elif old_field.db_index and new_field.db_index:
221+
index_names = self._constraint_names(model, [old_field.column], index=True)
222+
for index_name in index_names:
223+
self.execute(self._delete_constraint_sql(self.sql_delete_index, model, index_name))
208224
# Only if we have a default and there is a change from NULL to NOT NULL
209225
four_way_default_alteration = (
210226
new_field.has_default() and
@@ -260,7 +276,7 @@ def _alter_field(self, model, old_field, new_field, old_type, new_type,
260276
(not old_field.unique and new_field.unique)):
261277
self.execute(self._create_index_sql(model, [new_field], suffix="_uniq"))
262278
# Restore an index, SQL Server requires explicit restoration
263-
if old_type != new_type:
279+
if old_type != new_type or (old_field.null and not new_field.null):
264280
if old_field.unique and new_field.unique:
265281
self.execute(self._create_unique_sql(model, [new_field.column]))
266282
elif old_field.db_index and new_field.db_index:

0 commit comments

Comments
 (0)