#23611 closed Bug (fixed)
update_or_create doesn't retain related manager reference
Reported by: | Ryan Hiebert | Owned by: | André Ericson |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.7 |
Severity: | Normal | Keywords: | |
Cc: | Loic Bistuer | Triage Stage: | Accepted |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
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 1.7.x branch, and in the master branch.
Change History (16)
comment:1 by , 10 years ago
Description: | modified (diff) |
---|
comment:2 by , 10 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:3 by , 10 years ago
comment:4 by , 10 years ago
@kswiat I have submitted a pull request at https://github.com/django/django/pull/3321 but let me know if you have any suggestions.
comment:5 by , 10 years ago
Has patch: | set |
---|
comment:6 by , 10 years ago
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?
comment:7 by , 10 years ago
It qualifies for a backport if only if 1.7 is behaving differently than version 1.6. What happens in version 1.6?
comment:9 by , 10 years ago
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.
comment:10 by , 10 years ago
I'm not exactly sure how the process works neither.
Should I make a pull request against stable/1.7.x
?
I believe the fix should apply to that aswell.
comment:11 by , 10 years ago
Triage Stage: | Unreviewed → Accepted |
---|
Ahh. I forgot update_or_create was new. Not sure on backporting, but, in any case the pull request is correctly against master.
follow-up: 13 comment:12 by , 10 years ago
Cc: | added |
---|
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.
comment:13 by , 10 years ago
Replying to loic:
It's missing from
GenericRelatedObjectManager
; note thatGenericRelatedObjectManager.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
comment:15 by , 10 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
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.