Skip to content

Commit 22f87a1

Browse files
committed
Merge branch 'azure-1.11' into azure-2.0
2 parents 03f3063 + 1408935 commit 22f87a1

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

sql_server/pyodbc/schema.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,11 @@ def _alter_field(self, model, old_field, new_field, old_type, new_type,
208208
continue
209209
self.execute(self._delete_constraint_sql(self.sql_delete_index, model, index_name))
210210
# Change check constraints?
211-
if old_db_params['check'] != new_db_params['check'] and old_db_params['check']:
211+
if (old_db_params['check'] != new_db_params['check'] and old_db_params['check']) or (
212+
# SQL Server requires explicit deletion befor altering column type with the same constraint
213+
old_db_params['check'] == new_db_params['check'] and old_db_params['check'] and
214+
old_db_params['type'] != new_db_params['type']
215+
):
212216
constraint_names = self._constraint_names(model, [old_field.column], check=True)
213217
if strict and len(constraint_names) != 1:
214218
raise ValueError("Found wrong number (%s) of check constraints for %s.%s" % (
@@ -402,7 +406,11 @@ def _alter_field(self, model, old_field, new_field, old_type, new_type,
402406
if not rel.many_to_many and rel.field.db_constraint:
403407
self.execute(self._create_fk_sql(rel.related_model, rel.field, "_fk"))
404408
# Does it have check constraints we need to add?
405-
if old_db_params['check'] != new_db_params['check'] and new_db_params['check']:
409+
if (old_db_params['check'] != new_db_params['check'] and new_db_params['check']) or (
410+
# SQL Server requires explicit creation after altering column type with the same constraint
411+
old_db_params['check'] == new_db_params['check'] and new_db_params['check'] and
412+
old_db_params['type'] != new_db_params['type']
413+
):
406414
self.execute(
407415
self.sql_create_check % {
408416
"table": self.quote_name(model._meta.db_table),

0 commit comments

Comments
 (0)