Migration fails when removing explicit primary key
The automatic migration created by migrate fails when
- a table was created with a field where "primary_key=True" is specified.
- data is added to the table
- The model is modified so that no field is primary key, and Django auto-creates "id" field.
To reproduce the problem:
- create new model, set some field as primary key
- set up migrations with initial migration
- modify model so field is not primary key
- generate migration
Problems:
- will ask for default value for auto-generated ID field
- will try to create id field as primary key, before modifying existing key
Manual Fix:
- remove default from models.AutoField() call
- reverse order of operations so AlterField() comes before AddField()
I'm attaching a session log with a complete transcript.
Change History
(18)
Component: |
Database layer (models, ORM) → Migrations
|
Triage Stage: |
Unreviewed → Accepted
|
Type: |
Uncategorized → Bug
|
Cc: |
bcail added
|
Has patch: |
set
|
Owner: |
changed from nobody to bcail
|
Status: |
new → assigned
|
Patch needs improvement: |
set
|
Patch needs improvement: |
unset
|
Summary: |
Migration fails when removing explicit primary key (Postgres) → Migration fails when removing explicit primary key
|
Summary: |
Migration fails when removing explicit primary key → Migration fails when removing explicit primary key (Postgres)
|
Patch needs improvement: |
set
|
Summary: |
Migration fails when removing explicit primary key (Postgres) → Migration fails when removing explicit primary key
|
Patch needs improvement: |
unset
|
Triage Stage: |
Accepted → Ready for checkin
|
Patch needs improvement: |
set
|
Triage Stage: |
Ready for checkin → Accepted
|
Terminal session log demonstrating issue in 22997.