Opened 2 years ago

Last modified 19 months ago

#22997 new Bug

Migration fails when removing explicit primary key (Postgres)

Reported by: a.lloyd.flanagan@… Owned by: nobody
Component: Migrations Version: 1.7
Severity: Normal Keywords: migrate primary_key
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


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:

  1. create new model, set some field as primary key
  2. set up migrations with initial migration
  3. modify model so field is not primary key
  4. generate migration


  • 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.

Attachments (1)

django_session_log.txt (7.5 KB) - added by a.lloyd.flanagan@… 2 years ago.
Terminal session log demonstrating issue in 22997.

Download all attachments as: .zip

Change History (4)

Changed 2 years ago by a.lloyd.flanagan@…

Terminal session log demonstrating issue in 22997.

comment:1 Changed 2 years ago by timo

  • Component changed from Database layer (models, ORM) to Migrations
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted
  • Type changed from Uncategorized to Bug

I guess it's probably too much of an edge case to be a blocker for 1.7, but would be nice if it "just worked".

comment:2 Changed 19 months ago by timgraham

  • Version changed from 1.7-rc-1 to 1.7

comment:3 Changed 19 months ago by timgraham

#24030 seems related.

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