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 Simon Charette, 5 years ago

Component: MigrationsCore (System checks)
Keywords: E310 added
Triage Stage: UnreviewedAccepted

comment:2 by Mariusz Felisiak, 5 years ago

Keywords: E311 added
Summary: UniqueConstraint rises fields.E310 error because of issue with no backward compatibility with unique_togetherfields.E310-E311 should take into account UniqueConstraints without conditions.
Version: 2.2master

Agreed, both checks should take into UniqueConstraint's without condition's.

comment:3 by Ahmad Abdallah, 5 years ago

Owner: changed from nobody to Ahmad Abdallah
Status: newassigned

comment:4 by Mariusz Felisiak, 5 years ago

Easy pickings: set

comment:5 by Ahmad Abdallah, 5 years ago

Posting a patch soon

comment:6 by Ahmad Abdallah, 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 Ahmad Abdallah, 5 years ago

Has patch: set

comment:8 by Hasan Ramezani, 5 years ago

Needs tests: set

comment:9 by Mariusz Felisiak, 5 years ago

Patch needs improvement: set

comment:10 by Ahmad Abdallah, 5 years ago

Needs tests: unset
Patch needs improvement: unset

comment:11 by Mariusz Felisiak, 5 years ago

Needs documentation: set
Needs tests: set
Patch needs improvement: set

comment:12 by Ahmad Abdallah, 5 years ago

Needs tests: unset
Patch needs improvement: unset

comment:13 by Ahmad Abdallah, 5 years ago

Needs documentation: unset

comment:14 by Mariusz Felisiak, 5 years ago

Triage Stage: AcceptedReady for checkin

comment:15 by Mariusz Felisiak <felisiak.mariusz@…>, 5 years ago

Resolution: fixed
Status: assignedclosed

In 5bf28ac2:

Fixed #31185 -- Fixed detecting of unique fields in ForeignKey/ForeignObject checks when using Meta.constraints.

Note: See TracTickets for help on using tickets.
Back to Top