#23611: update_or_create doesn't retain related manager reference
----------------------------------------------+--------------------
     Reporter:  ryanhiebert                   |      Owner:  nobody
         Type:  Bug                           |     Status:  new
    Component:  Database layer (models, ORM)  |    Version:  1.7
     Severity:  Normal                        |   Keywords:
 Triage Stage:  Unreviewed                    |  Has patch:  0
Easy pickings:  0                             |      UI/UX:  0
----------------------------------------------+--------------------
 The update_or_create method on a related manager doesn't seem to retain
 the reference from the object that it came from, as I've come to expect
 from other methods (get, create, get_or_create). For example, the
 following models.py:

 {{{
 from django.db import models

 class Spam(models.Model):
     pass

 class Egg(models.Model):
     spam = models.ForeignKey(Spam, related_name='eggs')
 }}}

 And these tests:

 {{{
 from django.test import TestCase

 from .models import Spam, Egg

 class TestUpdateOrCreate(TestCase):
     def test_update_or_create_on_model_manager(self):
         spam = Spam.objects.create()
         Egg.objects.update_or_create(id=7, defaults={'spam': spam})

     def test_update_or_create_on_related_manager(self):
         spam = Spam.objects.create()
         spam.eggs.update_or_create(id=8)

     def test_create_on_related_manager(self):
         spam = Spam.objects.create()
         spam.eggs.create(id=9)

     def test_get_or_create_on_related_manager(self):
         spam = Spam.objects.create()
         spam.eggs.get_or_create(id=9)
 }}}

 All the tests but {{{test_update_or_create_on_related_manager}}} succeed.
 That one fails with an {{{IntegrityError}}}:

 {{{
 django.db.utils.IntegrityError: NOT NULL constraint failed:
 django_related_update_egg.spam_id
 }}}

 This is the case in the 1.7 release, the latest of the 17.x branch, and in
 the master branch.

--
Ticket URL: <https://code.djangoproject.com/ticket/23611>
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/054.58cc6dd6f276637f9da16ff1f78d91b3%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to