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

73 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 PM2/29/24
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 PM2/29/24
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 AM4/12/24
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 AM4/18/24
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 AM4/30/24
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 AM4/30/24
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 AM4/30/24
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>

Django

unread,
Sep 16, 2024, 2:51:23 AM9/16/24
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
-------------------------------------+------------------------------------
Comment (by Sarah Boyce):

#35761 is a duplicate
--
Ticket URL: <https://code.djangoproject.com/ticket/22997#comment:11>

Django

unread,
Oct 2, 2024, 5:11:45 PM10/2/24
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 bcail):

* needs_better_patch: 1 => 0

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

Django

unread,
Oct 2, 2024, 5:20:53 PM10/2/24
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
-------------------------------------+------------------------------------
Comment (by bcail):

[https://github.com/django/django/pull/18645 Another PR] that just
addresses part of this ticket.
--
Ticket URL: <https://code.djangoproject.com/ticket/22997#comment:13>

Django

unread,
Feb 5, 2025, 9:51:58 AM2/5/25
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: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* stage: Accepted => Ready for checkin

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

Django

unread,
Feb 6, 2025, 7:53:53 AM2/6/25
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: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Sarah Boyce <42296566+sarahboyce@…>):

In [changeset:"db7b1ae9f6d9e26facbb3da4cf5f5a513704bfe5" db7b1ae9]:
{{{#!CommitTicketReference repository=""
revision="db7b1ae9f6d9e26facbb3da4cf5f5a513704bfe5"
Refs #22997 -- Prevented requesting a default value for auto fields.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/22997#comment:15>

Django

unread,
Feb 6, 2025, 7:56:05 AM2/6/25
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: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Sarah Boyce <42296566+sarahboyce@…>):

In [changeset:"d57bf4618c15bc7e920fd4488b13907fe33eb786" d57bf461]:
{{{#!CommitTicketReference repository=""
revision="d57bf4618c15bc7e920fd4488b13907fe33eb786"
[5.2.x] Refs #22997 -- Prevented requesting a default value for auto
fields.

Backport of db7b1ae9f6d9e26facbb3da4cf5f5a513704bfe5 from main.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/22997#comment:16>

Django

unread,
Feb 6, 2025, 7:56:32 AM2/6/25
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
* stage: Ready for checkin => Accepted

--
Ticket URL: <https://code.djangoproject.com/ticket/22997#comment:17>
Reply all
Reply to author
Forward
0 new messages