Opened 6 years ago
Last modified 11 months ago
#28821 new New feature
Allow QuerySet.bulk_create() on multi-table inheritance when possible
Reported by: | Joey Wilhelm | Owned by: | |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Abhishek Gautam, Sardorbek Imomaliev, jon.dufresne@…, Shai Berger, Adam Johnson | Triage Stage: | Accepted |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | yes |
Easy pickings: | no | UI/UX: | no |
Description
According to this comment in bulk_create
:
# When you bulk insert you don't get the primary keys back (if it's an # autoincrement, except if can_return_ids_from_bulk_insert=True), so # you can't insert into the child tables which references this.
This implies that, if we do retrieve primary keys from the parent model's bulk insert, then it is possible to bulk insert into the child tables automatically.
Now that Django does have the ability to automatically retrieve, and set, primary keys on a bulk create operation, it would be nice to allow this use case when possible (specifically, when the backend has can_return_ids_from_bulk_insert=True
). Keying it off this feature would give PostgreSQL this ability immediately, and then let it work for Oracle as soon as retrieval of PKs is fully supported on that engine as well.
Also, regardless if Django does this automatically, I would like to be able to manually set the _ptr
fields on the child records in order to affect a bulk_create without the need for automatic retrieval of IDs. However, even that is not possible, as the bulk_create
method fails on multi-table inheritance in all cases.
Change History (11)
comment:1 Changed 6 years ago by
Summary: | Allow bulk_create on multi-table inheritance when possible → Allow QuerySet.bulk_create() on multi-table inheritance when possible |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:2 Changed 6 years ago by
Cc: | Abhishek Gautam added |
---|---|
Owner: | changed from nobody to Abhishek Gautam |
Status: | new → assigned |
comment:3 Changed 5 years ago by
Owner: | Abhishek Gautam deleted |
---|---|
Status: | assigned → new |
comment:4 Changed 4 years ago by
Cc: | Sardorbek Imomaliev added |
---|
comment:5 Changed 3 years ago by
comment:6 Changed 2 years ago by
Cc: | jon.dufresne@… added |
---|---|
Has patch: | set |
comment:7 Changed 2 years ago by
Owner: | set to Jon Dufresne |
---|---|
Status: | new → assigned |
comment:8 Changed 2 years ago by
Patch needs improvement: | set |
---|
comment:9 Changed 20 months ago by
Cc: | Shai Berger added |
---|
There's a naive implementation of a special case in the new broken-down-models library. Could be interesting to compare.
https://github.com/Matific/broken-down-models/blob/main/bdmodels/models.py#L114 (actual line number may have changed by the time you read this, of course)
comment:10 Changed 20 months ago by
Cc: | Adam Johnson added |
---|
comment:11 Changed 11 months ago by
Owner: | Jon Dufresne deleted |
---|---|
Status: | assigned → new |
This would be an excellent feature and we would really love to be able to use it.
In certain circumstances (think UUID PKs) it might also be possible, at least with Postgresql (don't know about the others), to do it the other way around. Foreign key integrity checks are deferred to the end of the transaction, so you could save all of the child tables first, and then save all the parent tables. As long as the entire operation is wrapped in a single transaction, it wouldn't matter that
_ptr
temporarily held an invalid FK.