Opened 5 years ago
Closed 5 years ago
#31185 closed Bug (fixed)
fields.E310-E311 should take into account UniqueConstraints without conditions.
Reported by: | Pavel Garkin | Owned by: | Ahmad Abdallah |
---|---|---|---|
Component: | Core (System checks) | Version: | dev |
Severity: | Normal | Keywords: | UniqueConstraint unique_together E310 E311 |
Cc: | Triage Stage: | Ready for checkin | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | yes | UI/UX: | no |
Description
Hello,
I'm trying to create migration with this kind of model.
class AppUsers(models.Model): name = models.CharField(...) uid = models.CharField(...) source = models.ForeignKey(...) class Meta: constraints = [models.UniqueConstraint(fields=['uid', 'source'], name='appusers_uniqueness')]
When I start makemigrations command in manage.py I've faced fields.E310 https://docs.djangoproject.com/en/2.2/ref/checks/#related-fields error
It says that I should add unique_together field in Meta options:
app_name.AppUsers.field: (fields.E310) No subset of the fields 'uid', 'source' on model 'AppUsers' is unique.
HINT: Add unique=True on any of those fields or add at least a subset of them to a unique_together constraint.
If I change Meta options to unique_together constraint migration passes with no errors.
class AppUsers(models.Model): name = models.CharField(...) uid = models.CharField(...) source = models.ForeignKey(...) class Meta: unique_together = [['uid', 'source']]
As mentioned in docs https://docs.djangoproject.com/en/2.2/ref/models/options/#unique-together unique_together may be deprecated in the future. So I think nobody wants to face this issue when this will be deprecated :)
Thanks,
Pavel
Change History (15)
comment:1 by , 5 years ago
Component: | Migrations → Core (System checks) |
---|---|
Keywords: | E310 added |
Triage Stage: | Unreviewed → Accepted |
comment:2 by , 5 years ago
Keywords: | E311 added |
---|---|
Summary: | UniqueConstraint rises fields.E310 error because of issue with no backward compatibility with unique_together → fields.E310-E311 should take into account UniqueConstraints without conditions. |
Version: | 2.2 → master |
comment:3 by , 5 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:4 by , 5 years ago
Easy pickings: | set |
---|
comment:6 by , 5 years ago
Patch submitted. Let me know what i need to improve. I haven't written a test it but I tested it on the code snippet posted above and it worked
comment:7 by , 5 years ago
Has patch: | set |
---|
comment:8 by , 5 years ago
Needs tests: | set |
---|
comment:9 by , 5 years ago
Patch needs improvement: | set |
---|
comment:10 by , 5 years ago
Needs tests: | unset |
---|---|
Patch needs improvement: | unset |
comment:11 by , 5 years ago
Needs documentation: | set |
---|---|
Needs tests: | set |
Patch needs improvement: | set |
comment:12 by , 5 years ago
Needs tests: | unset |
---|---|
Patch needs improvement: | unset |
comment:13 by , 5 years ago
Needs documentation: | unset |
---|
comment:14 by , 5 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
Agreed, both checks should take into
UniqueConstraint
's withoutcondition
's.