[Django] #22997: Migration fails when removing explicit primary key (Postgres)

28 views
Skip to first unread message

Django

unread,
Jul 10, 2014, 3:21:39 PM7/10/14
to django-...@googlegroups.com
#22997: Migration fails when removing explicit primary key (Postgres)
----------------------------------------------+----------------------------
Reporter: a.lloyd.flanagan@… | Owner: nobody
Type: Uncategorized | Status: new
Component: Database layer (models, ORM) | Version: 1.7-rc-1
Severity: Normal | Keywords: migrate
Triage Stage: Unreviewed | primary_key
Easy pickings: 0 | Has patch: 0
| UI/UX: 0
----------------------------------------------+----------------------------
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.

--
Ticket URL: <https://code.djangoproject.com/ticket/22997>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jul 10, 2014, 5:51:24 PM7/10/14
to django-...@googlegroups.com
#22997: Migration fails when removing explicit primary key (Postgres)
-------------------------------------+------------------------------------
Reporter: a.lloyd.flanagan@… | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 1.7-rc-1
Severity: Normal | Resolution:
Keywords: migrate primary_key | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+------------------------------------
Changes (by timo):

* needs_better_patch: => 0
* component: Database layer (models, ORM) => Migrations
* needs_tests: => 0
* needs_docs: => 0
* type: Uncategorized => Bug
* stage: Unreviewed => Accepted


Comment:

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

--
Ticket URL: <https://code.djangoproject.com/ticket/22997#comment:1>

Django

unread,
Dec 18, 2014, 4:47:32 PM12/18/14
to django-...@googlegroups.com
#22997: Migration fails when removing explicit primary key (Postgres)
-------------------------------------+------------------------------------
Reporter: a.lloyd.flanagan@… | Owner: nobody

Type: Bug | Status: new
Component: Migrations | Version: 1.7
Severity: Normal | Resolution:
Keywords: migrate primary_key | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+------------------------------------
Changes (by timgraham):

* version: 1.7-rc-1 => 1.7


--
Ticket URL: <https://code.djangoproject.com/ticket/22997#comment:2>

Django

unread,
Dec 22, 2014, 11:58:36 AM12/22/14
to django-...@googlegroups.com
#22997: Migration fails when removing explicit primary key (Postgres)
-------------------------------------+------------------------------------
Reporter: a.lloyd.flanagan@… | Owner: nobody

Type: Bug | Status: new
Component: Migrations | Version: 1.7
Severity: Normal | Resolution:
Keywords: migrate primary_key | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+------------------------------------

Comment (by timgraham):

#24030 seems related.

--
Ticket URL: <https://code.djangoproject.com/ticket/22997#comment:3>

Django

unread,
Feb 29, 2024, 1:21:08 PMFeb 29
to django-...@googlegroups.com
#22997: Migration fails when removing explicit primary key (Postgres)
-------------------------------------+------------------------------------
Reporter: a.lloyd.flanagan@… | Owner: bcail
Type: Bug | Status: assigned
Component: Migrations | Version: 1.7
Severity: Normal | Resolution:
Keywords: migrate primary_key | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+------------------------------------
Changes (by bcail):

* cc: bcail (added)
* has_patch: 0 => 1
* owner: nobody => bcail
* status: new => assigned

Comment:

I opened [https://github.com/django/django/pull/17923 a PR] for one of the
problems - where a default is added to an AutoField.
--
Ticket URL: <https://code.djangoproject.com/ticket/22997#comment:4>

Django

unread,
Feb 29, 2024, 4:40:12 PMFeb 29
to django-...@googlegroups.com
#22997: Migration fails when removing explicit primary key (Postgres)
-------------------------------------+------------------------------------
Reporter: a.lloyd.flanagan@… | Owner: bcail
Type: Bug | Status: assigned
Component: Migrations | Version: 1.7
Severity: Normal | Resolution:
Keywords: migrate primary_key | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+------------------------------------
Comment (by bcail):

I updated the PR to handle the other issue as well - to generate altered
fields before added fields.
--
Ticket URL: <https://code.djangoproject.com/ticket/22997#comment:5>

Django

unread,
Apr 12, 2024, 3:39:44 AMApr 12
to django-...@googlegroups.com
#22997: Migration fails when removing explicit primary key (Postgres)
-------------------------------------+------------------------------------
Reporter: a.lloyd.flanagan@… | Owner: bcail
Type: Bug | Status: assigned
Component: Migrations | Version: 1.7
Severity: Normal | Resolution:
Keywords: migrate primary_key | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+------------------------------------
Changes (by Bhuvnesh):

* needs_better_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/22997#comment:6>

Django

unread,
Apr 18, 2024, 11:02:44 AMApr 18
to django-...@googlegroups.com
#22997: Migration fails when removing explicit primary key (Postgres)
-------------------------------------+------------------------------------
Reporter: a.lloyd.flanagan@… | Owner: bcail
Type: Bug | Status: assigned
Component: Migrations | Version: 1.7
Severity: Normal | Resolution:
Keywords: migrate primary_key | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+------------------------------------
Changes (by bcail):

* needs_better_patch: 1 => 0

--
Ticket URL: <https://code.djangoproject.com/ticket/22997#comment:7>

Django

unread,
Apr 30, 2024, 6:16:01 AMApr 30
to django-...@googlegroups.com
#22997: Migration fails when removing explicit primary key
-------------------------------------+------------------------------------
Reporter: a.lloyd.flanagan@… | Owner: bcail
Type: Bug | Status: assigned
Component: Migrations | Version: 1.7
Severity: Normal | Resolution:
Keywords: migrate primary_key | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+------------------------------------
Changes (by Sarah Boyce):

* summary: Migration fails when removing explicit primary key (Postgres)
=> Migration fails when removing explicit primary key

Comment:

(I can replicate also on SQLite so removing Postgres reference as it does
not only relate to Postgres)
--
Ticket URL: <https://code.djangoproject.com/ticket/22997#comment:8>

Django

unread,
Apr 30, 2024, 6:22:46 AMApr 30
to django-...@googlegroups.com
#22997: Migration fails when removing explicit primary key (Postgres)
-------------------------------------+------------------------------------
Reporter: a.lloyd.flanagan@… | Owner: bcail
Type: Bug | Status: assigned
Component: Migrations | Version: 1.7
Severity: Normal | Resolution:
Keywords: migrate primary_key | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+------------------------------------
Changes (by Sarah Boyce):

* summary: Migration fails when removing explicit primary key => Migration
fails when removing explicit primary key (Postgres)

Comment:

(Ah perhaps the fix as described is only related to Postgres? Can confirm
the same migrations and prompting happen on other databases).
--
Ticket URL: <https://code.djangoproject.com/ticket/22997#comment:9>

Django

unread,
Apr 30, 2024, 10:49:51 AMApr 30
to django-...@googlegroups.com
#22997: Migration fails when removing explicit primary key
-------------------------------------+------------------------------------
Reporter: a.lloyd.flanagan@… | Owner: bcail
Type: Bug | Status: assigned
Component: Migrations | Version: 1.7
Severity: Normal | Resolution:
Keywords: migrate primary_key | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+------------------------------------
Changes (by Sarah Boyce):

* needs_better_patch: 0 => 1
* summary: Migration fails when removing explicit primary key (Postgres)
=> Migration fails when removing explicit primary key

Comment:

#35416 is a duplicate.
Marking "Patch needs improvement" as the case, as described in #35416,
needs to be handled.
--
Ticket URL: <https://code.djangoproject.com/ticket/22997#comment:10>
Reply all
Reply to author
Forward
0 new messages