{{{
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.
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0
Old description:
New description:
{{{
from django.db import models
class Spam(models.Model):
pass
And these tests:
{{{
from django.test import TestCase
This is the case in the 1.7 release, the latest of the 1.7.x branch, and
in the master branch.
--
--
Ticket URL: <https://code.djangoproject.com/ticket/23611#comment:1>
* status: new => assigned
* owner: nobody => aericson
--
Ticket URL: <https://code.djangoproject.com/ticket/23611#comment:2>
Comment (by kswiat):
I would like to work on this one too, if You (aericson) have nothing
against it. I have experienced similar problems with related managers
recently and have done already some research.
--
Ticket URL: <https://code.djangoproject.com/ticket/23611#comment:3>
Comment (by aericson):
@kswiat I have submitted a pull request at
https://github.com/django/django/pull/3321 but let me know if you have any
suggestions.
--
Ticket URL: <https://code.djangoproject.com/ticket/23611#comment:4>
* has_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/23611#comment:5>
Comment (by ryanhiebert):
Thank you @aericson. The PR is against master, and I'm unfamiliar with the
process. How do changes like this that affect 1.7 get backported to the
1.7 branch? Would this one qualify?
--
Ticket URL: <https://code.djangoproject.com/ticket/23611#comment:6>
Comment (by collinanderson):
It qualifies for a backport if only if 1.7 is behaving differently than
version 1.6. What happens in version 1.6?
--
Ticket URL: <https://code.djangoproject.com/ticket/23611#comment:7>
Comment (by aericson):
I believe update_or_create was added at 1.7, wasn't it?
--
Ticket URL: <https://code.djangoproject.com/ticket/23611#comment:8>
Comment (by ryanhiebert):
{{{update_or_create}}} was added in 1.7, but the behavior fixed in this
patch has precedent in 1.6 with {{{get}}}, {{{get_or_create}}},
{{{create}}}, and others.
--
Ticket URL: <https://code.djangoproject.com/ticket/23611#comment:9>
Comment (by aericson):
I'm not exactly sure how the process works neither.
Should I make a merge request against {{{ stable/1.7.x}}}?
I believe the fix should apply to that aswell.
--
Ticket URL: <https://code.djangoproject.com/ticket/23611#comment:10>
* stage: Unreviewed => Accepted
Comment:
Ahh. I forgot update_or_create was new. Not sure on backporting, but, in
any case the pull request is correctly against master.
--
Ticket URL: <https://code.djangoproject.com/ticket/23611#comment:11>
* cc: loic (added)
Comment:
It's missing from `GenericRelatedObjectManager`; note that
`GenericRelatedObjectManager.get_or_create()` is missing too.
Since it's a bug in a new feature, I think it's worthy of a backport.
--
Ticket URL: <https://code.djangoproject.com/ticket/23611#comment:12>
Comment (by aericson):
Replying to [comment:12 loic]:
> It's missing from `GenericRelatedObjectManager`; note that
`GenericRelatedObjectManager.get_or_create()` is missing too.
>
> Since it's a bug in a new feature, I think it's worthy of a backport.
I'll be adding them to GenericRelatedObjectManager. Thanks
--
Ticket URL: <https://code.djangoproject.com/ticket/23611#comment:13>
Comment (by aericson):
Pull request updated.
--
Ticket URL: <https://code.djangoproject.com/ticket/23611#comment:14>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"ed37f7e979186c99a6f351c289eb486461601d80"]:
{{{
#!CommitTicketReference repository=""
revision="ed37f7e979186c99a6f351c289eb486461601d80"
Fixed #23611 -- update_or_create failing from a related manager
Added update_or_create to RelatedManager, ManyRelatedManager and
GenericRelatedObjectManager.
Added missing get_or_create to GenericRelatedObjectManager.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/23611#comment:15>
Comment (by Loic Bistuer <loic.bistuer@…>):
In [changeset:"fa4b6482df08d308fe88044b8c8bf981c6225fb8"]:
{{{
#!CommitTicketReference repository=""
revision="fa4b6482df08d308fe88044b8c8bf981c6225fb8"
[1.7.x] Fixed #23611 -- update_or_create failing from a related manager
Added update_or_create to RelatedManager, ManyRelatedManager and
GenericRelatedObjectManager.
Added missing get_or_create to GenericRelatedObjectManager.
Conflicts:
tests/generic_relations/tests.py
tests/get_or_create/tests.py
Backport of ed37f7e979 from master
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/23611#comment:16>