Opened 4 years ago

Closed 4 years ago

#32133 closed Bug (duplicate)

bulk_create of parent after setting on child leads to unexpected data loss

Reported by: Hannes Ljungberg Owned by: Hannes Ljungberg
Component: Database layer (models, ORM) Version: 3.1
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I think this is a missed case of https://code.djangoproject.com/ticket/28147. The behaviour is exactly the same but affects bulk_update.

Failing testcase:

parent = NoFields()
child = NullableFields(auto_field=parent)
NoFields.objects.bulk_create([parent])
NullableFields.objects.bulk_create([child])
child.refresh_from_db()
self.assertEqual(child.auto_field, parent)

This behaviour was also described in https://code.djangoproject.com/ticket/29497 but might’ve been missed when closed as a duplicate.

My proposal is to extract the code in Model.save() which ensures that all fields are in sync before save to it’s own method which can then be used by bulk_update.

Change History (2)

comment:1 by Hannes Ljungberg, 4 years ago

Has patch: set

comment:2 by Mariusz Felisiak, 4 years ago

Resolution: duplicate
Status: assignedclosed

Thanks, let's reopen the original report.

Duplicate of #29497.

Note: See TracTickets for help on using tickets.
Back to Top