summaryrefslogtreecommitdiff
path: root/docs/topics
diff options
context:
space:
mode:
authorClifford Gama <cliffygamy@gmail.com>2025-02-10 09:52:27 +0200
committerSarah Boyce <42296566+sarahboyce@users.noreply.github.com>2025-03-04 13:06:09 +0100
commitae2736ca3bf4c6a27e23ee95530ad965b550d4cc (patch)
tree4b20178ac5f620ac2a5e6dd3f2eea2bdd3aa5f5e /docs/topics
parentc09bceef68e5abb79accedd12dade16aa6577a09 (diff)
Fixed #36128 -- Clarified auto-generated unique constraint on m2m through models.
Diffstat (limited to 'docs/topics')
-rw-r--r--docs/topics/db/models.txt12
1 files changed, 12 insertions, 0 deletions
diff --git a/docs/topics/db/models.txt b/docs/topics/db/models.txt
index 415d3829dd..b7bd36bc81 100644
--- a/docs/topics/db/models.txt
+++ b/docs/topics/db/models.txt
@@ -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