1
0
mirror of https://github.com/django/django.git synced 2025-10-23 21:59:11 +00:00

Fixed #36128 -- Clarified auto-generated unique constraint on m2m through models.

This commit is contained in:
Clifford Gama
2025-02-10 09:52:27 +02:00
committed by Sarah Boyce
parent c09bceef68
commit ae2736ca3b
3 changed files with 22 additions and 8 deletions

View File

@@ -503,10 +503,22 @@ something like this::
date_joined = models.DateField()
invite_reason = models.CharField(max_length=64)
class Meta:
constraints = [
models.UniqueConstraint(
fields=["person", "group"], name="unique_person_group"
)
]
When you set up the intermediary model, you explicitly specify foreign
keys to the models that are involved in the many-to-many relationship. This
explicit declaration defines how the two models are related.
If you don't want multiple associations between the same instances, add a
:class:`~django.db.models.UniqueConstraint` including the ``from`` and ``to``
fields. Django's automatically generated many-to-many tables include such a
constraint.
There are a few restrictions on the intermediate model:
* Your intermediate model must contain one - and *only* one - foreign key