The get parts tries to get an instance of m2 which is related to m1, which
does not exists. this leads to creating a new instance of m2 with field
equals the value used above. this already exists and leads to a
IntegrityError.
See this minimal example:
http://pastebin.com/eUs1R6fD
--
Ticket URL: <https://code.djangoproject.com/ticket/23673>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* status: new => closed
* needs_better_patch: => 0
* resolution: => worksforme
* needs_tests: => 0
* needs_docs: => 0
Comment:
Hi,
It seems to me that things are working as intended.
In your example, when you use `m.m2m`, you basically get a queryset that's
equivalent to `M2.objects.filter(m1=m)`.
So when you do `m.m2m.get_or_create(name='foo')`, you're doing
M2.objects.filter(m1=m).get_or_create(name='foo')`.
If there's a M2 element with a name `foo` but which doesn't appear in
`m.m2m`, then by definition `get_or_create` will attempty to create it.
This is of course not possible because of the unique constraint on the
name, so you get the `IntegrityError`.
Closing this as `worksforme`. Please reopen if I've misunderstood your
issue.
Thanks.
--
Ticket URL: <https://code.djangoproject.com/ticket/23673#comment:1>
Comment (by jo-soft):
Hey,
no, you got me. But I expected that in line 15 there should be called an
``add`` with the existing ``foo`` on ``another_m`` s.t. in the end ``m``
and ``another_m`` refer to the same M2 instance with the unique name
``foo`` instead of creating a another instance of ``M2`` with name foo.
bg,
Johannes
--
Ticket URL: <https://code.djangoproject.com/ticket/23673#comment:2>