#23673: IntegrityError when using a ManyToMany relation to add a model instance
with unique constraint.
-------------------------------------+-------------------------------------
     Reporter:  jo-soft              |                    Owner:  nobody
         Type:  Bug                  |                   Status:  closed
    Component:  Database layer       |                  Version:  1.7
  (models, ORM)                      |               Resolution:
     Severity:  Normal               |  worksforme
     Keywords:  ManyToManyField,     |             Triage Stage:
  get_or_create                      |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by bmispelon):

 * 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>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/065.b874f82cc8f0c4f68f3c46167d5e9fc5%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to