[Django] #33322: bulk_update not working with foreign key when referenced model was saved after the assignment.

275 views
Skip to first unread message

Django

unread,
Nov 26, 2021, 9:39:56 AM11/26/21
to django-...@googlegroups.com
#33322: bulk_update not working with foreign key when referenced model was saved
after the assignment.
-------------------------------------+-------------------------------------
Reporter: | Owner: nobody
GwynBleidD |
Type: Bug | Status: new
Component: Database | Version: 3.2
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Consider following example:

{{{
class Child(models.Model):
pass


class Parent(models.Model):
child = models.ForeignKey(Child, on_delete=models.CASCADE, null=True)
}}}

{{{
parent = Parent.objects.create(child=None)

parent.child = Child()
parent.child.save()

Parent.objects.bulk_update([parent], fields=["child"])
}}}

Expected behavior:
parent model instance was updated with the ID to the child in the
database.

Actual behavior:
Parent model is still referencing Null.

There should probably be some check for ForeignKeys in the bulk_update
logic, and if one is updated, the ID of the child model should be re-
copied to the child_id field that is actually being written to the
database.

--
Ticket URL: <https://code.djangoproject.com/ticket/33322>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Nov 26, 2021, 10:47:52 AM11/26/21
to django-...@googlegroups.com
#33322: Saving parent object after setting on child leads to unexpected data loss
in bulk_update().
-------------------------------------+-------------------------------------
Reporter: GwynBleidD | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* cc: Hannes Ljungberg (added)
* stage: Unreviewed => Accepted


Comment:

Thanks for the report. This is because `child_id` is not updated after
`parent.child.save()`.

Related to #29497 and #32133.

--
Ticket URL: <https://code.djangoproject.com/ticket/33322#comment:1>

Django

unread,
Nov 27, 2021, 9:56:44 AM11/27/21
to django-...@googlegroups.com
#33322: Saving parent object after setting on child leads to unexpected data loss
in bulk_update().
-------------------------------------+-------------------------------------
Reporter: GwynBleidD | Owner: Hannes
| Ljungberg
Type: Bug | Status: assigned

Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Hannes Ljungberg):

* owner: nobody => Hannes Ljungberg
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/33322#comment:2>

Django

unread,
Nov 27, 2021, 11:33:12 AM11/27/21
to django-...@googlegroups.com
#33322: Saving parent object after setting on child leads to unexpected data loss
in bulk_update().
-------------------------------------+-------------------------------------
Reporter: GwynBleidD | Owner: Hannes
| Ljungberg
Type: Bug | Status: assigned
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Hannes Ljungberg):

* has_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/33322#comment:3>

Django

unread,
Nov 27, 2021, 1:13:22 PM11/27/21
to django-...@googlegroups.com
#33322: Saving parent object after setting on child leads to unexpected data loss
in bulk_update().
-------------------------------------+-------------------------------------
Reporter: GwynBleidD | Owner: Hannes
| Ljungberg
Type: Bug | Status: assigned
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Egor R):

* cc: Egor R (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/33322#comment:4>

Django

unread,
Nov 29, 2021, 12:32:17 AM11/29/21
to django-...@googlegroups.com
#33322: Saving parent object after setting on child leads to unexpected data loss
in bulk_update().
-------------------------------------+-------------------------------------
Reporter: GwynBleidD | Owner: Hannes
| Ljungberg
Type: Bug | Status: assigned
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/33322#comment:5>

Django

unread,
Nov 29, 2021, 1:03:38 AM11/29/21
to django-...@googlegroups.com
#33322: Saving parent object after setting on child leads to unexpected data loss
in bulk_update().
-------------------------------------+-------------------------------------
Reporter: GwynBleidD | Owner: Hannes
| Ljungberg
Type: Bug | Status: closed

Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"ed2018037d152eef7e68f339b4562f8aadc2b7a0" ed201803]:
{{{
#!CommitTicketReference repository=""
revision="ed2018037d152eef7e68f339b4562f8aadc2b7a0"
Fixed #33322 -- Fixed loss of assigned related object when saving relation
with bulk_update().
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/33322#comment:6>

Django

unread,
Jun 2, 2022, 2:39:03 AM6/2/22
to django-...@googlegroups.com
#33322: Saving parent object after setting on child leads to unexpected data loss
in bulk_update().
-------------------------------------+-------------------------------------
Reporter: GwynBleidD | Owner: Hannes
| Ljungberg
Type: Bug | Status: closed
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Manuel Baclet):

The PR was merged 6 months ago but it has not landed in any release. Can
someone add this to the current bugfix branch, please?

--
Ticket URL: <https://code.djangoproject.com/ticket/33322#comment:7>

Django

unread,
Jun 2, 2022, 2:47:52 AM6/2/22
to django-...@googlegroups.com
#33322: Saving parent object after setting on child leads to unexpected data loss
in bulk_update().
-------------------------------------+-------------------------------------
Reporter: GwynBleidD | Owner: Hannes
| Ljungberg
Type: Bug | Status: closed
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak):

Replying to [comment:7 Manuel Baclet]:


> The PR was merged 6 months ago but it has not landed in any release. Can
someone add this to the current bugfix branch, please?

This will be released in Django 4.1+.

--
Ticket URL: <https://code.djangoproject.com/ticket/33322#comment:8>

Reply all
Reply to author
Forward
0 new messages