[Django] #35962: Migration crashes when attempting to remove constraint on already removed foreign key

25 views
Skip to first unread message

Django

unread,
Dec 1, 2024, 9:55:19 PM12/1/24
to django-...@googlegroups.com
#35962: Migration crashes when attempting to remove constraint on already removed
foreign key
-----------------------------+--------------------------------------
Reporter: Jacob Walls | Type: Bug
Status: new | Component: Migrations
Version: dev | Severity: Normal
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------+--------------------------------------
Same issue as #34333, just for `RemoveField` & `RemoveConstraint` instead
of `AddField` and `AddConstraint`.

Reproduction:
Repeat steps 1-4 of #34333.
5. Restore models.py to step 1 and make a third migration.

Migration will look like:
{{{#!python
# Generated by Django 5.2.dev20241129120448 on 2024-12-01 20:41

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("other", "0002_category_model_category_and_more"),
]

operations = [
migrations.RemoveField(
model_name="model",
name="category",
),
migrations.RemoveConstraint(
model_name="model",
name="unique_category_for_date",
),
migrations.DeleteModel(
name="Category",
),
]
}}}

And will crash as follows:
{{{#!python
django.db.utils.ProgrammingError: constraint "unique_category_for_date" of
relation "other_model" does not exist
}}}

Succeeds if the `RemoveConstraint` and `RemoveField` are manually
reordered.
Tested on Postgres 16.
--
Ticket URL: <https://code.djangoproject.com/ticket/35962>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Dec 1, 2024, 11:50:25 PM12/1/24
to django-...@googlegroups.com
#35962: Migration crashes when attempting to remove constraint on already removed
foreign key
-----------------------------+------------------------------------
Reporter: Jacob Walls | Owner: (none)
Type: Bug | Status: new
Component: Migrations | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------+------------------------------------
Changes (by Simon Charette):

* stage: Unreviewed => Accepted

Comment:

Thanks, this should be solvable by a similar approach to dependency as
4b1bfea2846f66f504265cec46ee1fe94ee9c98b.

That's another can of worms but we should also add a reduce for
`RemoveIndex` and `RemoveConstraint` against `DeleteModel`.
--
Ticket URL: <https://code.djangoproject.com/ticket/35962#comment:1>

Django

unread,
Dec 2, 2024, 8:08:50 AM12/2/24
to django-...@googlegroups.com
#35962: Migration crashes when attempting to remove constraint on already removed
foreign key
-----------------------------+--------------------------------------------
Reporter: Jacob Walls | Owner: Emmanuel Sandjio
Type: Bug | Status: assigned
Component: Migrations | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------+--------------------------------------------
Changes (by Emmanuel Sandjio):

* owner: (none) => Emmanuel Sandjio
* status: new => assigned

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

Django

unread,
Apr 8, 2025, 1:49:06 PM4/8/25
to django-...@googlegroups.com
#35962: Migration crashes when attempting to remove constraint on already removed
foreign key
-----------------------------+--------------------------------------------
Reporter: Jacob Walls | Owner: Emmanuel Sandjio
Type: Bug | Status: assigned
Component: Migrations | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------+--------------------------------------------
Comment (by Simon Charette):

#35595 has
[https://github.com/django/django/compare/main...charettes:django:ticket-35595
a solution] that addressed both issues.
--
Ticket URL: <https://code.djangoproject.com/ticket/35962#comment:3>

Django

unread,
May 4, 2025, 6:37:57 AM5/4/25
to django-...@googlegroups.com
#35962: Migration crashes when attempting to remove constraint on already removed
foreign key
-----------------------------+------------------------------------
Reporter: Jacob Walls | Owner: wookkl
Type: Bug | Status: assigned
Component: Migrations | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------+------------------------------------
Changes (by Jacob Walls):

* has_patch: 0 => 1
* owner: Emmanuel Sandjio => wookkl

Comment:

[https://github.com/django/django/pull/19361 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/35962#comment:4>

Django

unread,
May 9, 2025, 10:04:29 AM5/9/25
to django-...@googlegroups.com
#35962: Migration crashes when attempting to remove constraint on already removed
foreign key
-----------------------------+------------------------------------
Reporter: Jacob Walls | Owner: wookkl
Type: Bug | Status: assigned
Component: Migrations | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------+------------------------------------
Changes (by Sarah Boyce):

* needs_tests: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/35962#comment:5>

Django

unread,
Jun 27, 2025, 6:08:20 AM6/27/25
to django-...@googlegroups.com
#35962: Migration crashes when attempting to remove constraint on already removed
foreign key
-----------------------------+---------------------------------------------
Reporter: Jacob Walls | Owner: wookkl
Type: Bug | Status: assigned
Component: Migrations | Version: dev
Severity: Normal | Resolution:
Keywords: | 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):

* needs_tests: 1 => 0
* stage: Accepted => Ready for checkin

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

Django

unread,
Jun 27, 2025, 11:17:56 AM6/27/25
to django-...@googlegroups.com
#35962: Migration crashes when attempting to remove constraint on already removed
foreign key
-----------------------------+---------------------------------------------
Reporter: Jacob Walls | Owner: wookkl
Type: Bug | Status: closed
Component: Migrations | Version: dev
Severity: Normal | Resolution: fixed
Keywords: | 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 <42296566+sarahboyce@…>):

* resolution: => fixed
* status: assigned => closed

Comment:

In [changeset:"29f5e1e97d660855d34c1ee5edbd5cfe094b6ca7" 29f5e1e9]:
{{{#!CommitTicketReference repository=""
revision="29f5e1e97d660855d34c1ee5edbd5cfe094b6ca7"
Fixed #35595, #35962 -- Removed indexes and constraints before fields in
migrations.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/35962#comment:7>
Reply all
Reply to author
Forward
0 new messages