summaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
authorAymeric Augustin <aymeric.augustin@m4x.org>2013-05-19 05:21:39 -0700
committerAymeric Augustin <aymeric.augustin@m4x.org>2013-05-19 05:21:39 -0700
commitc28b79580493f4bf5df9bea88a5c6bddfe27a6d6 (patch)
treea5d498bf2a020c84902698b8175f4eb03ee6ead8 /docs
parentd34b1c29e294b19e51a47918125314a1540c01d4 (diff)
parent65f9e0affd8ca04e2c597c43c1547ef7c888ec2a (diff)
Merge pull request #1164 from pyriku/get_or_create_through_m2m
Integrity problems when using get_or_create through M2M
Diffstat (limited to 'docs')
-rw-r--r--docs/ref/models/querysets.txt35
1 files changed, 35 insertions, 0 deletions
diff --git a/docs/ref/models/querysets.txt b/docs/ref/models/querysets.txt
index 9677b321c6..2dec00afc1 100644
--- a/docs/ref/models/querysets.txt
+++ b/docs/ref/models/querysets.txt
@@ -1409,6 +1409,41 @@ has a side effect on your data. For more, see `Safe methods`_ in the HTTP spec.
.. _Safe methods: http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.1.1
+.. warning::
+
+ You can use ``get_or_create()`` through :class:`~django.db.models.ManyToManyField`
+ attributes and reverse relations. In that case you will restrict the queries
+ inside the context of that relation. That could lead you to some integrity
+ problems if you don't use it consistently.
+
+ Being the following models::
+
+ class Chapter(models.Model):
+ title = models.CharField(max_length=255, unique=True)
+
+ class Book(models.Model):
+ title = models.CharField(max_length=256)
+ chapters = models.ManyToManyField(Chapter)
+
+ You can use ``get_or_create()`` through Book's chapters field, but it only
+ fetches inside the context of that book::
+
+ >>> book = Book.objects.create(title="Ulysses")
+ >>> book.chapters.get_or_create(title="Telemachus")
+ (<Chapter: Telemachus>, True)
+ >>> book.chapters.get_or_create(title="Telemachus")
+ (<Chapter: Telemachus>, False)
+ >>> Chapter.objects.create(title="Chapter 1")
+ <Chapter: Chapter 1>
+ >>> book.chapters.get_or_create(title="Chapter 1")
+ # Raises IntegrityError
+
+ This is happening because it's trying to get or create "Chapter 1" through the
+ book "Ulysses", but it can't do any of them: the relation can't fetch that
+ chapter because it isn't related to that book, but it can't create it either
+ because ``title`` field should be unique.
+
+
bulk_create
~~~~~~~~~~~