﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
36947	Changing a field's db_comment unnecessarily alters column type	Alex Fischer	Clifford Gama	"(On databases that support altering the comment independently of the type.)

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"").
"	Bug	assigned	Migrations	6.0	Normal				Accepted	1	0	0	0	0	0
