Tartempion34
Tartempion34

Reputation: 600

Django UniqueConstraint doesn't apply

I'm on Django 2.2 and try to aplly a UniqueConstraint with conditions. But It seems that it's not applied, I still can add the entries breaking this constraint.

My simplified model:

class Size(models.Model):
    name = models.CharField(max_length=200, null=False)
    matching_equipments = models.ManyToManyField('Equipment', through=Equipment.sizes.through, blank=True,related_name='matching_sizes')
    is_active = models.BooleanField(default=True)

    class Meta:
        default_permissions = ()
        ordering = ['id']
        constraints = [
            models.UniqueConstraint(fields=['name'], condition=Q(is_active=True), name='unique_sizename_active')
        ]

The objective is to never have 2 sizes with the same name active at the same time.

The migrations run well but nothing is applied on the database (mySQL)

Any idea?

Upvotes: 1

Views: 1937

Answers (1)

Kevin Christopher Henry
Kevin Christopher Henry

Reputation: 48902

The documentation states that:

The condition argument is ignored with MySQL and MariaDB as neither supports conditional indexes.

Upvotes: 8

Related Questions