UniqueConstraint rises fields.E310 error because of issue with backward compatibility with unique_together

14 views
Skip to first unread message

Pavel Garkin

unread,
Jan 20, 2020, 7:21:26 AM1/20/20
to Django users
Hi all,


I'm trying to create migration with uniqueness of 2 fields: uid, source. 

Django 2.2.9

class Users(models.Model):
uid = models.CharField(
...)
source = models.ForeignKey(
...)

class Meta:
       constraints = [models.UniqueConstraint(fields=['uid', 'source'], name='users_uniqueness')]
indexes = [models.Index(fields=('uid', 'source'), name='users_indexes')]


When I start makemigrations command in manage.py it rises fields.E310 error

app_name.Users.field: (fields.E310) No subset of the fields 'uid', 'source' on model 'Users' is unique.
	HINT: Add unique=True on any of those fields or add at least a subset of them to a unique_together constraint.


When I change Meta options to unique_together constraint it works ok. Migrations passes with no errors

...
   class Meta:
        unique_together = [['uid', 'source']]

As mentioned in docs unique_together may be deprecated in the future so I wanted to avoid this kind of issue.


Thanks,
Pavel

Simon Charette

unread,
Jan 20, 2020, 9:01:04 AM1/20/20
to Django users
Hello Pavel,

This is likely a bug because UniqueConstraint was only recently introduced.

Please file a bug report about it. In the mean time you can add this check to
your SILENCED_SYSTEM_CHECKS setting to silence it.

Best,
Simon
Reply all
Reply to author
Forward
0 new messages