Opened 10 years ago
Last modified 10 years ago
#23611 closed Bug
update_or_create doesn't retain related manager reference — at Version 1
Reported by: | Ryan Hiebert | Owned by: | nobody |
---|---|---|---|
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.
Note:
See TracTickets
for help on using tickets.