Skip to content

Alter ForeignKey from non-nullable to nullable, including data migration, in squashed migration, leads to duplicate index created #77

@fvkluck

Description

@fvkluck

Hi all,

I recently ran into an issue when squashing migrations. The issue is that there is a duplicate index in the database. We're probably going to work around it by skipping step 3 below, but I thought it still makes sense to create an issue, in the case someone else runs into it.

I managed to find a minimal reproducible example and uploaded it here

The steps I took to create this example:

  1. Create new django project and new app
  2. Create ModelA and ModelB, where ModelB has a nullable ForeignKey relation to ModelA, generate migrations
  3. Create a data migration where you create some objects of ModelA and perform some filter(..).update(...)s on ModelB
  4. Alter the ForeignKey from ModelB to ModelA to be non-nullable. Generate migrations, selecting option 2 (don't specify a default) when prompted by Django.
  5. Squash these migrations

The result in the database is:
image
There are two indexes here, where I would expect only 1.

The peculiar thing is that without step 3, only 1 unique index is created (as I would expect). I haven't investigated if the issue also occurs when you perform less or different creations/updates in step 3.
The issue doesn't occur when using sqlite as backend, but it also occurs with the django-mssql-backend.

Best,
Florian

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions