Opened 10 years ago

Last modified 4 weeks ago

#22997 assigned Bug

Migration fails when removing explicit primary key

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

Description

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

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.

Attachments (1)

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

Download all attachments as: .zip

Change History (11)

by a.lloyd.flanagan@…, 10 years ago

Attachment: django_session_log.txt added

Terminal session log demonstrating issue in 22997.

comment:1 by Tim Graham, 10 years ago

Component: Database layer (models, ORM)Migrations
Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

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 by Tim Graham, 9 years ago

Version: 1.7-rc-11.7

comment:3 by Tim Graham, 9 years ago

#24030 seems related.

comment:4 by bcail, 3 months ago

Cc: bcail added
Has patch: set
Owner: changed from nobody to bcail
Status: newassigned

I opened a PR for one of the problems - where a default is added to an AutoField.

comment:5 by bcail, 3 months ago

I updated the PR to handle the other issue as well - to generate altered fields before added fields.

comment:6 by Bhuvnesh, 6 weeks ago

Patch needs improvement: set

comment:7 by bcail, 5 weeks ago

Patch needs improvement: unset

comment:8 by Sarah Boyce, 4 weeks ago

Summary: Migration fails when removing explicit primary key (Postgres)Migration fails when removing explicit primary key

(I can replicate also on SQLite so removing Postgres reference as it does not only relate to Postgres)

comment:9 by Sarah Boyce, 4 weeks ago

Summary: Migration fails when removing explicit primary keyMigration fails when removing explicit primary key (Postgres)

(Ah perhaps the fix as described is only related to Postgres? Can confirm the same migrations and prompting happen on other databases).

comment:10 by Sarah Boyce, 4 weeks ago

Patch needs improvement: set
Summary: Migration fails when removing explicit primary key (Postgres)Migration fails when removing explicit primary key

#35416 is a duplicate.
Marking "Patch needs improvement" as the case, as described in #35416, needs to be handled.

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