@@ -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