Skip to content

Commit 1408935

Browse files
committed
fix the issue michiya#127
1 parent 329c431 commit 1408935

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
@@ -136,7 +136,11 @@ def _alter_field(self, model, old_field, new_field, old_type, new_type,
136136
continue
137137
self.execute(self._delete_constraint_sql(self.sql_delete_index, model, index_name))
138138
# Change check constraints?
139-
if old_db_params['check'] != new_db_params['check'] and old_db_params['check']:
139+
if (old_db_params['check'] != new_db_params['check'] and old_db_params['check']) or (
140+
# SQL Server requires explicit deletion befor altering column type with the same constraint
141+
old_db_params['check'] == new_db_params['check'] and old_db_params['check'] and
142+
old_db_params['type'] != new_db_params['type']
143+
):
140144
constraint_names = self._constraint_names(model, [old_field.column], check=True)
141145
if strict and len(constraint_names) != 1:
142146
raise ValueError("Found wrong number (%s) of check constraints for %s.%s" % (
@@ -365,7 +369,11 @@ def _alter_field(self, model, old_field, new_field, old_type, new_type,
365369
if not rel.many_to_many and rel.field.db_constraint:
366370
self.execute(self._create_fk_sql(rel.related_model, rel.field, "_fk"))
367371
# Does it have check constraints we need to add?
368-
if old_db_params['check'] != new_db_params['check'] and new_db_params['check']:
372+
if (old_db_params['check'] != new_db_params['check'] and new_db_params['check']) or (
373+
# SQL Server requires explicit creation after altering column type with the same constraint
374+
old_db_params['check'] == new_db_params['check'] and new_db_params['check'] and
375+
old_db_params['type'] != new_db_params['type']
376+
):
369377
self.execute(
370378
self.sql_create_check % {
371379
"table": self.quote_name(model._meta.db_table),

0 commit comments

Comments
 (0)