Browse Source

Fixed #35839 -- Fixed crash when adding GeneratedField with db_comment on MySQL.

Thanks Simon Charette for the test.

Signed-off-by: saJaeHyukc <wogur981208@gmail.com>
saJaeHyukc 1 month ago
parent
commit
f7017db92c
2 changed files with 20 additions and 2 deletions
  1. 2 2
      django/db/backends/base/schema.py
  2. 18 0
      tests/schema/tests.py

+ 2 - 2
django/db/backends/base/schema.py

@@ -313,8 +313,6 @@ class BaseDatabaseSchemaEditor:
         yield column_db_type
         if collation := field_db_params.get("collation"):
             yield self._collate_sql(collation)
-        if self.connection.features.supports_comments_inline and field.db_comment:
-            yield self._comment_sql(field.db_comment)
         # Work out nullability.
         null = field.null
         # Add database default.
@@ -373,6 +371,8 @@ class BaseDatabaseSchemaEditor:
             and field.unique
         ):
             yield self.connection.ops.tablespace_sql(tablespace, inline=True)
+        if self.connection.features.supports_comments_inline and field.db_comment:
+            yield self._comment_sql(field.db_comment)
 
     def column_sql(self, model, field, include_default=False):
         """

+ 18 - 0
tests/schema/tests.py

@@ -4861,6 +4861,24 @@ class SchemaTests(TransactionTestCase):
             comment,
         )
 
+    @skipUnlessDBFeature("supports_comments", "supports_stored_generated_columns")
+    def test_add_db_comment_generated_field(self):
+        comment = "Custom comment"
+        field = GeneratedField(
+            expression=Value(1),
+            db_persist=True,
+            output_field=IntegerField(),
+            db_comment=comment,
+        )
+        field.set_attributes_from_name("volume")
+        with connection.schema_editor() as editor:
+            editor.create_model(Author)
+            editor.add_field(Author, field)
+        self.assertEqual(
+            self.get_column_comment(Author._meta.db_table, "volume"),
+            comment,
+        )
+
     @skipUnlessDBFeature("supports_comments")
     def test_add_db_comment_and_default_charfield(self):
         comment = "Custom comment with default"