diff options
| author | Clifford Gama <cliffygamy@gmail.com> | 2025-02-10 09:52:27 +0200 |
|---|---|---|
| committer | Sarah Boyce <42296566+sarahboyce@users.noreply.github.com> | 2025-03-04 13:08:01 +0100 |
| commit | cc405e154649af37e1741aeb4438391943133c29 (patch) | |
| tree | 0ee9944b86d3e124d923c7b9d15ce857ea01cff1 /docs/topics | |
| parent | 03ace756eafc3f7a85f5b84d28ce1e7ce092c018 (diff) | |
[5.1.x] Fixed #36128 -- Clarified auto-generated unique constraint on m2m through models.
Backport of ae2736ca3bf4c6a27e23ee95530ad965b550d4cc from main.
Diffstat (limited to 'docs/topics')
| -rw-r--r-- | docs/topics/db/models.txt | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/docs/topics/db/models.txt b/docs/topics/db/models.txt index 62488a75f7..b543fd6759 100644 --- a/docs/topics/db/models.txt +++ b/docs/topics/db/models.txt @@ -507,10 +507,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 |
