Opened 10 months ago

Closed 3 months ago

#35962 closed Bug (fixed)

Migration crashes when attempting to remove constraint on already removed foreign key

Reported by: Jacob Walls Owned by: wookkl
Component: Migrations Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Same issue as #34333, just for RemoveField & RemoveConstraint instead of AddField and AddConstraint.

Reproduction:
Repeat steps 1-4 of #34333.

  1. Restore models.py to step 1 and make a third migration.

Migration will look like:

# 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:

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.

Change History (7)

comment:1 by Simon Charette, 10 months ago

Triage Stage: UnreviewedAccepted

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 in a possible follow up ticket.

Last edited 10 months ago by Simon Charette (previous) (diff)

comment:2 by Emmanuel Sandjio, 10 months ago

Owner: set to Emmanuel Sandjio
Status: newassigned

comment:3 by Simon Charette, 6 months ago

#35595 has a solution that addressed both issues.

comment:4 by Jacob Walls, 5 months ago

Has patch: set
Owner: changed from Emmanuel Sandjio to wookkl

comment:5 by Sarah Boyce, 5 months ago

Needs tests: set

comment:6 by Sarah Boyce, 3 months ago

Needs tests: unset
Triage Stage: AcceptedReady for checkin

comment:7 by Sarah Boyce <42296566+sarahboyce@…>, 3 months ago

Resolution: fixed
Status: assignedclosed

In 29f5e1e9:

Fixed #35595, #35962 -- Removed indexes and constraints before fields in migrations.

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