Skip to content

Alter unique, nullable field to non-unique, nullable fails #100

@rubendv

Description

@rubendv

When a field is nullable and unique, a filtered index is created to enforce uniqueness instead of a unique constraint.
But when the field is made non-unique and still nullable, the generated SQL uses DROP CONSTRAINT instead of DROP INDEX, which doesn't work.

In our case this happened when changing this model:

class MyModel(models.Model):
    field1= models.CharField(max_length=254, null=True, unique=True)
    field2 = models.ForeignKey('myapp.othermodel', null=False, on_delete=models.deletion.CASCADE)

To this model:

class MyModel(models.Model):
    field1= models.CharField(max_length=254, null=True)
    field2 = models.ForeignKey('myapp.othermodel', null=False, on_delete=models.deletion.CASCADE)

    class Meta:
        unique_together = ('field2', 'field1')

Probably also happens without the unique_together but by just setting unique=False.

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