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
--
Ticket URL: <https://code.djangoproject.com/ticket/31185>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* keywords: UniqueConstraint, unique_together => UniqueConstraint
unique_together E310
* component: Migrations => Core (System checks)
* stage: Unreviewed => Accepted
--
Ticket URL: <https://code.djangoproject.com/ticket/31185#comment:1>
* keywords: UniqueConstraint unique_together E310 => UniqueConstraint
unique_together E310 E311
* version: 2.2 => master
Comment:
Agreed, both checks should take into `UniqueConstraint`'s without
`condition`'s.
--
Ticket URL: <https://code.djangoproject.com/ticket/31185#comment:2>
* owner: nobody => Ahmad Abdallah
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/31185#comment:3>
* easy: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/31185#comment:4>
Comment (by Ahmad Abdallah):
Posting a patch soon
--
Ticket URL: <https://code.djangoproject.com/ticket/31185#comment:5>
Comment (by Ahmad Abdallah):
[https://github.com/django/django/pull/12477 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
--
Ticket URL: <https://code.djangoproject.com/ticket/31185#comment:6>
* has_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/31185#comment:7>
* needs_tests: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/31185#comment:8>
* needs_better_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/31185#comment:9>
* needs_better_patch: 1 => 0
* needs_tests: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/31185#comment:10>
* needs_better_patch: 0 => 1
* needs_tests: 0 => 1
* needs_docs: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/31185#comment:11>
* needs_better_patch: 1 => 0
* needs_tests: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/31185#comment:12>
* needs_docs: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/31185#comment:13>
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/31185#comment:14>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"5bf28ac2ed17773214bd6c7796ef356067c9fa91" 5bf28ac2]:
{{{
#!CommitTicketReference repository=""
revision="5bf28ac2ed17773214bd6c7796ef356067c9fa91"
Fixed #31185 -- Fixed detecting of unique fields in
ForeignKey/ForeignObject checks when using Meta.constraints.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/31185#comment:15>