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 , 4 years ago
Has patch: | set |
---|
comment:2 by , 4 years ago
Resolution: | → duplicate |
---|---|
Status: | assigned → closed |
Thanks, let's reopen the original report.
Duplicate of #29497.
PR: https://github.com/django/django/pull/13589