[Django] #35839: GeneratedField with db_comment causes syntax error

22 views
Skip to first unread message

Django

unread,
Oct 14, 2024, 2:07:14 PM10/14/24
to django-...@googlegroups.com
#35839: GeneratedField with db_comment causes syntax error
-------------------------------------+-------------------------------------
Reporter: Jason Christa | Type: Bug
Status: new | Component: Database
| layer (models, ORM)
Version: 5.0 | Severity: Normal
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Adding a new GeneratedField with the db_comment argument cause invalid SQL
to be generated for MySQL (maybe others but that is all I tested).
Altering a GeneratedField with a db_comment seems to work fine.
--
Ticket URL: <https://code.djangoproject.com/ticket/35839>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Oct 14, 2024, 2:50:23 PM10/14/24
to django-...@googlegroups.com
#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

unread,
Oct 14, 2024, 2:54:46 PM10/14/24
to django-...@googlegroups.com
#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
-------------------------------+------------------------------------
Comment (by Simon Charette):

Jason, If you're interested in submitting a patch it seems that the
solution lies in changing the order of the generated SQL in
`_iter_column_sql`.
--
Ticket URL: <https://code.djangoproject.com/ticket/35839#comment:2>

Django

unread,
Oct 14, 2024, 5:01:34 PM10/14/24
to django-...@googlegroups.com
#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 Mariusz Felisiak):

* cc: Mariusz Felisiak (added)

--
Ticket URL: <https://code.djangoproject.com/ticket/35839#comment:3>

Django

unread,
Oct 14, 2024, 10:43:53 PM10/14/24
to django-...@googlegroups.com
#35839: GeneratedField with db_comment causes syntax error
-------------------------------+-------------------------------------
Reporter: Jason Christa | Owner: JasonCalm
Type: Bug | Status: assigned
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 JasonCalm):

* owner: (none) => JasonCalm
* status: new => assigned

Comment:

I'd like to try resolving this ticket!
--
Ticket URL: <https://code.djangoproject.com/ticket/35839#comment:4>

Django

unread,
Oct 30, 2024, 12:53:51 PM10/30/24
to django-...@googlegroups.com
#35839: GeneratedField with db_comment causes syntax error
-------------------------------+-------------------------------------
Reporter: Jason Christa | Owner: JasonCalm
Type: Bug | Status: assigned
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
-------------------------------+-------------------------------------
Comment (by Mariusz Felisiak):

Replying to [comment:4 JasonCalm]:
> I'd like to try resolving this ticket!

Jason, are you working on this? If not, I'd be happy to pass this to my
Djangonauts.
--
Ticket URL: <https://code.djangoproject.com/ticket/35839#comment:5>

Django

unread,
Nov 1, 2024, 1:58:42 AM11/1/24
to django-...@googlegroups.com
#35839: GeneratedField with db_comment causes syntax error
-------------------------------+-------------------------------------
Reporter: Jason Christa | Owner: JasonCalm
Type: Bug | Status: assigned
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
-------------------------------+-------------------------------------
Comment (by JasonCalm):

Replying to [comment:5 Mariusz Felisiak]:
> Replying to [comment:4 JasonCalm]:
> > I'd like to try resolving this ticket!
>
> Jason, are you working on this? If not, I'd be happy to pass this to my
Djangonauts.

Yes, I'm currently working on this part!
--
Ticket URL: <https://code.djangoproject.com/ticket/35839#comment:6>

Django

unread,
Feb 20, 2025, 3:37:39 AMFeb 20
to django-...@googlegroups.com
#35839: GeneratedField with db_comment causes syntax error
-------------------------------+--------------------------------------
Reporter: Jason Christa | Owner: JaeHyuckSa
Type: Bug | Status: assigned
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 JaeHyuckSa):

* owner: JasonCalm => JaeHyuckSa

--
Ticket URL: <https://code.djangoproject.com/ticket/35839#comment:7>

Django

unread,
Feb 20, 2025, 4:47:23 AMFeb 20
to django-...@googlegroups.com
#35839: GeneratedField with db_comment causes syntax error
-------------------------------+--------------------------------------
Reporter: Jason Christa | Owner: JaeHyuckSa
Type: Bug | Status: assigned
Component: Migrations | Version: 5.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by JaeHyuckSa):

* has_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/35839#comment:8>

Django

unread,
Feb 22, 2025, 11:17:47 AMFeb 22
to django-...@googlegroups.com
#35839: GeneratedField with db_comment causes syntax error
-------------------------------------+-------------------------------------
Reporter: Jason Christa | Owner:
| JaeHyuckSa
Type: Bug | Status: assigned
Component: Migrations | Version: 5.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* stage: Accepted => Ready for checkin

--
Ticket URL: <https://code.djangoproject.com/ticket/35839#comment:9>

Django

unread,
Feb 22, 2025, 1:57:03 PMFeb 22
to django-...@googlegroups.com
#35839: GeneratedField with db_comment causes syntax error
-------------------------------------+-------------------------------------
Reporter: Jason Christa | Owner:
| JaeHyuckSa
Type: Bug | Status: closed
Component: Migrations | Version: 5.0
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* resolution: => fixed
* status: assigned => closed

Comment:

In [changeset:"f7017db92cee1536c014710f475a613792c1e4b5" f7017db]:
{{{#!CommitTicketReference repository=""
revision="f7017db92cee1536c014710f475a613792c1e4b5"
Fixed #35839 -- Fixed crash when adding GeneratedField with db_comment on
MySQL.

Thanks Simon Charette for the test.

Signed-off-by: saJaeHyukc <wogur...@gmail.com>
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/35839#comment:10>
Reply all
Reply to author
Forward
0 new messages