[Django] #36947: Changing db_comment / GeneratedField error

5 views
Skip to first unread message

Django

unread,
Feb 24, 2026, 1:03:06 PM (10 days ago) Feb 24
to django-...@googlegroups.com
#36947: Changing db_comment / GeneratedField error
------------------------------+-----------------------------------------
Reporter: Alex Fischer | Type: Uncategorized
Status: new | Component: Uncategorized
Version: 6.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
------------------------------+-----------------------------------------
Django==5.2.11
Postgres 16.2

With this model definition:

{{{
class Foo(models.Model):
foo = models.IntegerField()
foo_copy = models.GeneratedField(
expression=models.F("foo"),
output_field=models.IntegerField(),
db_persist=True,
)
}}}

makemigrations and migrate run fine.

Now add a db_comment to foo:
{{{
class Foo(models.Model):
foo = models.IntegerField(db_comment="foo")
foo_copy = models.GeneratedField(
expression=models.F("foo"),
output_field=models.IntegerField(),
db_persist=True,
)
}}}

Now if we makemigrations and migrate again, we get the following error:
{{{
django.db.utils.NotSupportedError: cannot alter type of a column used by a
generated column
DETAIL: Column "foo" is used by generated column "foo_copy".
}}}

Running sqlmigrate on the migration that was produced, we can see what the
migration is trying to perform:
{{{
BEGIN;
--
-- Alter field foo on foo
--
ALTER TABLE "my_app_foo" ALTER COLUMN "foo" TYPE integer;
COMMENT ON COLUMN "issue_tracker_foo"."foo" IS 'foo';
COMMIT;
}}}

I don't know why it runs the first ALTER COLUMN statement. The ALTER
COLUMN statement is a no-op most of the time, and that's fine. But in this
case, it throws an error in Postgres ("cannot alter type of a column used
by a generated column").
--
Ticket URL: <https://code.djangoproject.com/ticket/36947>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Feb 25, 2026, 7:12:21 AM (10 days ago) Feb 25
to django-...@googlegroups.com
#36947: Changing db_comment / GeneratedField error
------------------------------+-----------------------------------------
Reporter: Alex Fischer | Owner: Clifford Gama
Type: Bug | Status: assigned
Component: Migrations | Version: 6.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 Clifford Gama):

* component: Uncategorized => Migrations
* owner: (none) => Clifford Gama
* stage: Unreviewed => Accepted
* status: new => assigned
* type: Uncategorized => Bug

Comment:

Thanks for the ticket. I don't think we should be altering the column type
when it hasn't changed. I'll investigate a fix.
--
Ticket URL: <https://code.djangoproject.com/ticket/36947#comment:1>
Reply all
Reply to author
Forward
0 new messages