Opened 7 months ago

Closed 7 days ago

Last modified 3 days ago

#36947 closed Bug (fixed)

Changing a field's db_comment unnecessarily alters column type

Reported by: Alex Fischer Owned by: Clifford Gama
Component: Migrations Version: 6.0
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by 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").

Change History (7)

comment:1 by Clifford Gama, 7 months ago

Component: UncategorizedMigrations
Owner: set to Clifford Gama
Status: newassigned
Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

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.

comment:2 by Clifford Gama, 7 months ago

Description: modified (diff)
Summary: Changing db_comment / GeneratedField errorChanging a field's db_comment unnecessarily alters column type

comment:3 by Clifford Gama, 7 months ago

Has patch: set

comment:4 by Adam Johnson, 5 weeks ago

Triage Stage: AcceptedReady for checkin

comment:5 by Sarah Boyce <42296566+sarahboyce@…>, 7 days ago

Resolution: fixed
Status: assignedclosed

In 64edef37:

Fixed #36947 -- Avoided altering column type when only db_comment changes.

Backends that allow altering column comments without altering the column
type should set supports_independent_comment_alteration=True.

Thanks Alex Fischer for the report.

Co-authored-by: Adam Johnson <me@…>

comment:6 by Jacob Walls <jacobtylerwalls@…>, 4 days ago

In a3f0642:

Refs #36947 -- Added missing max_length in GeneratedField test for Oracle.

This was missed because the model was defined inline without running system checks.

Follow-up to 64edef37e7b419dd584307d84650a192fb47dc4c.

comment:7 by Jacob Walls <jacobtylerwalls@…>, 3 days ago

In 82f34e2:

Refs #36947 -- Added supports_comments skip to test_alter_generated_field_base_field_comment.

Note: See TracTickets for help on using tickets.
Back to Top