@@ -105,19 +105,10 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
105105 @level0type = N'SCHEMA', @level0name = N'dbo',
106106 @level1type = N'TABLE', @level1name = %(table)s;"""
107107
108- sql_alter_column_comment = """IF NOT EXISTS (SELECT NULL FROM INFORMATION_SCHEMA.COLUMNS i
109- INNER JOIN sys.columns t ON t.name = i.COLUMN_NAME
110- LEFT JOIN sys.extended_properties ep ON t.object_id = ep.major_id
111- WHERE (ep.name = N'MS_Description' AND ep.minor_id = 2))
112- EXECUTE sp_addextendedproperty @name = N'MS_Description', @value = %(comment)s,
108+ sql_alter_column_comment = """EXECUTE sp_addextendedproperty @name = N'MS_Description', @value = %(comment)s,
113109 @level0type = N'SCHEMA', @level0name = N'dbo',
114110 @level1type = N'TABLE', @level1name = %(table)s,
115- @level2type = N'COLUMN', @level2name = %(column)s
116- ELSE
117- EXECUTE sp_updateextendedproperty @name = N'MS_Description', @value = %(comment)s,
118- @level0type = N'SCHEMA', @level0name = N'dbo',
119- @level1type = N'TABLE', @level1name = %(table)s,
120- @level2type = N'COLUMN', @level2name = %(column)s; """
111+ @level2type = N'COLUMN', @level2name = %(column)s"""
121112
122113 _deferred_unique_indexes = defaultdict (list )
123114
@@ -341,11 +332,11 @@ def _alter_field(self, model, old_field, new_field, old_type, new_type,
341332
342333 # Drop any FK constraints, we'll remake them later
343334 fks_dropped = set ()
344- if old_field .remote_field and old_field .db_constraint and self ._field_should_be_altered (
335+ if old_field .remote_field and old_field .db_constraint and ( django_version >= ( 4 , 2 ) and self ._field_should_be_altered (
345336 old_field ,
346337 new_field ,
347338 ignore = {"db_comment" },
348- ):
339+ )) :
349340 # Drop index, SQL Server requires explicit deletion
350341 if not hasattr (new_field , 'db_constraint' ) or not new_field .db_constraint :
351342 index_names = self ._constraint_names (model , [old_field .column ], index = True )
@@ -475,8 +466,8 @@ def _alter_field(self, model, old_field, new_field, old_type, new_type,
475466 actions = []
476467 null_actions = []
477468 post_actions = []
478- # Type change?
479- if old_type != new_type or (
469+ # Type or comment change?
470+ if old_type != new_type or (django_version >= ( 4 , 2 ) and
480471 self .connection .features .supports_comments
481472 and old_field .db_comment != new_field .db_comment
482473 ):
@@ -1161,7 +1152,7 @@ def create_model(self, model):
11611152 # Prevent using [] as params, in the case a literal '%' is used in the definition
11621153 self .execute (sql , params or None )
11631154
1164- if self .connection .features .supports_comments :
1155+ if django_version >= ( 4 , 2 ) and self .connection .features .supports_comments :
11651156 # Add table comment.
11661157 if model ._meta .db_table_comment :
11671158 self .alter_db_table_comment (model , None , model ._meta .db_table_comment )
0 commit comments