@@ -93,15 +93,40 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
9393 sql_rename_table = "EXEC sp_rename %(old_table)s, %(new_table)s"
9494 sql_create_unique_null = "CREATE UNIQUE INDEX %(name)s ON %(table)s(%(columns)s) " \
9595 "WHERE %(columns)s IS NOT NULL"
96- sql_alter_table_comment = """EXECUTE sp_addextendedproperty @name = N'MS_Description', @value = %(comment)s,
97- @level0type = N'SCHEMA', @level0name = N'dbo',
98- @level1type = N'TABLE', @level1name = %(table)s"""
99-
100- sql_alter_column_comment = """EXECUTE sp_addextendedproperty @name = N'MS_Description', @value = %(comment)s,
101- @level0type = N'SCHEMA', @level0name = N'dbo',
102- @level1type = N'TABLE', @level1name = %(table)s,
103- @level2type = N'COLUMN', @level2name = %(column)s"""
104-
96+ sql_alter_table_comment = """
97+ IF NOT EXISTS (SELECT NULL FROM sys.extended_properties ep
98+ WHERE ep.major_id = OBJECT_ID('%(table)s')
99+ AND ep.name = 'MS_Description'
100+ AND ep.minor_id = 0)
101+ EXECUTE sp_addextendedproperty
102+ @name = 'MS_Description', @value = %(comment)s,
103+ @level0type = 'SCHEMA', @level0name = 'dbo',
104+ @level1type = 'TABLE', @level1name = %(table)s
105+ ELSE
106+ EXECUTE sp_updateextendedproperty
107+ @name = 'MS_Description', @value = %(comment)s,
108+ @level0type = 'SCHEMA', @level0name = 'dbo',
109+ @level1type = 'TABLE', @level1name = %(table)s
110+ """
111+ sql_alter_column_comment = """
112+ IF NOT EXISTS (SELECT NULL FROM sys.extended_properties ep
113+ WHERE ep.major_id = OBJECT_ID('%(table)s')
114+ AND ep.name = 'MS_Description'
115+ AND ep.minor_id = (SELECT column_id FROM sys.columns
116+ WHERE name = '%(column)s'
117+ AND object_id = OBJECT_ID('%(table)s')))
118+ EXECUTE sp_addextendedproperty
119+ @name = 'MS_Description', @value = %(comment)s,
120+ @level0type = 'SCHEMA', @level0name = 'dbo',
121+ @level1type = 'TABLE', @level1name = %(table)s,
122+ @level2type = 'COLUMN', @level2name = %(column)s
123+ ELSE
124+ EXECUTE sp_updateextendedproperty
125+ @name = 'MS_Description', @value = %(comment)s,
126+ @level0type = 'SCHEMA', @level0name = 'dbo',
127+ @level1type = 'TABLE', @level1name = %(table)s,
128+ @level2type = 'COLUMN', @level2name = %(column)s
129+ """
105130 _deferred_unique_indexes = defaultdict (list )
106131
107132 def _alter_column_default_sql (self , model , old_field , new_field , drop = False ):
@@ -172,6 +197,8 @@ def _alter_column_null_sql(self, model, old_field, new_field):
172197 if django_version >= (4 , 2 ):
173198 def _alter_column_type_sql (self , model , old_field , new_field , new_type , old_collation , new_collation ):
174199 new_type = self ._set_field_new_type_null_status (old_field , new_type )
200+ # Check if existing
201+ # Drop exisiting
175202 return super ()._alter_column_type_sql (model , old_field , new_field , new_type , old_collation , new_collation )
176203 else :
177204 def _alter_column_type_sql (self , model , old_field , new_field , new_type ):
@@ -926,17 +953,17 @@ def add_field(self, model, field):
926953 }
927954 self .execute (sql , params )
928955 # Add field comment, if required.
929- # if (
930- # field.db_comment
931- # and self.connection.features.supports_comments
932- # and not self.connection.features.supports_comments_inline
933- # ):
934- # field_type = db_params["type"]
935- # self.execute(
936- # *self._alter_column_comment_sql(
937- # model, field, field_type, field.db_comment
938- # )
939- # )
956+ if (
957+ field .db_comment
958+ and self .connection .features .supports_comments
959+ and not self .connection .features .supports_comments_inline
960+ ):
961+ field_type = db_params ["type" ]
962+ self .execute (
963+ * self ._alter_column_comment_sql (
964+ model , field , field_type , field .db_comment
965+ )
966+ )
940967 # Add an index, if required
941968 self .deferred_sql .extend (self ._field_indexes_sql (model , field ))
942969 # Add any FK constraints later
@@ -1149,18 +1176,18 @@ def create_model(self, model):
11491176 if model ._meta .db_table_comment :
11501177 self .alter_db_table_comment (model , None , model ._meta .db_table_comment )
11511178 # Add column comments.
1152- # if not self.connection.features.supports_comments_inline:
1153- # for field in model._meta.local_fields:
1154- # if field.db_comment:
1155- # field_db_params = field.db_parameters(
1156- # connection=self.connection
1157- # )
1158- # field_type = field_db_params["type"]
1159- # self.execute(
1160- # *self._alter_column_comment_sql(
1161- # model, field, field_type, field.db_comment
1162- # )
1163- # )
1179+ if not self .connection .features .supports_comments_inline :
1180+ for field in model ._meta .local_fields :
1181+ if field .db_comment :
1182+ field_db_params = field .db_parameters (
1183+ connection = self .connection
1184+ )
1185+ field_type = field_db_params ["type" ]
1186+ self .execute (
1187+ * self ._alter_column_comment_sql (
1188+ model , field , field_type , field .db_comment
1189+ )
1190+ )
11641191 # Add any field index and index_together's (deferred as SQLite3 _remake_table needs it)
11651192 self .deferred_sql .extend (self ._model_indexes_sql (model ))
11661193 self .deferred_sql = list (set (self .deferred_sql ))
@@ -1335,6 +1362,3 @@ def _create_index_name(self, table_name, column_names, suffix=""):
13351362 new_index_name = index_name .replace ('[' , '' ).replace (']' , '' ).replace ('.' , '_' )
13361363 return new_index_name
13371364 return index_name
1338-
1339- def _alter_column_comment_sql (self , model , new_field , new_type , new_db_comment ):
1340- return "" , []
0 commit comments