Opened 2 years ago
Last modified 2 years ago
#33919 closed Bug
Primary keys added in non-initial migration are not created as identity columns in PostgreSQL — at Initial Version
Reported by: | jackcbrown89 | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 4.1 |
Severity: | Release blocker | Keywords: | postgres |
Cc: | Florian Apolloner, Michael Kany | Triage Stage: | Accepted |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
In this example, a model was created initially with a primary key on a one-to-one field. The primary key was then moved to a new AutoField. This was working prior to the 4.1 release.
## models.py class Example(models.Model): old_id = models.OneToOneField( OtherModel, on_delete=models.CASCADE, ## primary_key=True, <--- used to be the primary key ) id = models.AutoField(primary_key=True) # <--- new primary key
Running sqlmigrate on the migration where the primary key changes reveals that the "id" column does not autogenerate values:
$ python manage.py sqlmigrate example 0002 BEGIN; -- -- Add field id to example -- ALTER TABLE "example" ADD COLUMN "id" integer NOT NULL PRIMARY KEY; -- -- Alter field old_id on example -- ALTER TABLE "example" ALTER COLUMN "old_id" DROP NOT NULL;
When it should be
$ python manage.py sqlmigrate example 0002 BEGIN; -- -- Add field id to example -- ALTER TABLE "example" ADD COLUMN "id" integer NOT NULL PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY; -- -- Alter field old_id on example -- ALTER TABLE "example" ALTER COLUMN "old_id" DROP NOT NULL;
This seems to be related to the changes made here: https://code.djangoproject.com/ticket/30511
Note:
See TracTickets
for help on using tickets.