#35839: GeneratedField with db_comment causes syntax error
-------------------------------+------------------------------------
     Reporter:  Jason Christa  |                    Owner:  (none)
         Type:  Bug            |                   Status:  new
    Component:  Migrations     |                  Version:  5.0
     Severity:  Normal         |               Resolution:
     Keywords:                 |             Triage Stage:  Accepted
    Has patch:  0              |      Needs documentation:  0
  Needs tests:  0              |  Patch needs improvement:  0
Easy pickings:  0              |                    UI/UX:  0
-------------------------------+------------------------------------
Changes (by Simon Charette):

 * component:  Database layer (models, ORM) => Migrations
 * stage:  Unreviewed => Accepted

Comment:

 Reproduced with the following test

 {{{#!diff
 diff --git a/tests/schema/tests.py b/tests/schema/tests.py
 index 33a4bc527b..1dbbd5fe02 100644
 --- a/tests/schema/tests.py
 +++ b/tests/schema/tests.py
 @@ -4861,6 +4861,24 @@ def test_add_db_comment_charfield(self):
              comment,
          )

 +    @skipUnlessDBFeature("supports_comments")
 +    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"
 }}}

 It appears that the problem is due to the order the the `COMMENT` and
 `GENERATED` clause in the generated SQL.

 MySQL expects `COMMENT` to come after `GENERATED` while we do the opposite

 {{{#!python
 ALTER TABLE `schema_author` ADD COLUMN `volume` integer COMMENT 'Custom
 comment' GENERATED ALWAYS AS (1) STORED
 }}}

 Crashes while

 {{{#!python
 ALTER TABLE `schema_author` ADD COLUMN `volume` integer GENERATED ALWAYS
 AS (1) STORED COMMENT 'Custom comment'
 }}}

 passes.
-- 
Ticket URL: <https://code.djangoproject.com/ticket/35839#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/010701928c5dea85-d7458de7-f160-43ac-a546-3f525e8cbe98-000000%40eu-central-1.amazonses.com.

Reply via email to