Opened 2 hours ago

Last modified 32 minutes ago

#35962 new Bug

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

Reported by: Jacob Walls Owned by:
Component: Migrations Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no 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 (1)

comment:1 by Simon Charette, 32 minutes 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.

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