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 Tim Graham

Summary: Allow bulk_create on multi-table inheritance when possibleAllow QuerySet.bulk_create() on multi-table inheritance when possible
Triage Stage: UnreviewedAccepted

comment:2 Changed 6 years ago by Abhishek Gautam

Cc: Abhishek Gautam added
Owner: changed from nobody to Abhishek Gautam
Status: newassigned

comment:3 Changed 5 years ago by Abhishek Gautam

Owner: Abhishek Gautam deleted
Status: assignednew

comment:4 Changed 4 years ago by Sardorbek Imomaliev

Cc: Sardorbek Imomaliev added

comment:5 Changed 3 years ago by Daniel Alley

This would be an excellent feature and we would really love to be able to use it.

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.

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.

comment:6 Changed 2 years ago by Jon Dufresne

Cc: jon.dufresne@… added
Has patch: set

comment:7 Changed 2 years ago by Mariusz Felisiak

Owner: set to Jon Dufresne
Status: newassigned

comment:8 Changed 2 years ago by Mariusz Felisiak

Patch needs improvement: set

comment:9 Changed 20 months ago by Shai Berger

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 Adam Johnson

Cc: Adam Johnson added

comment:11 Changed 11 months ago by Mariusz Felisiak

Owner: Jon Dufresne deleted
Status: assignednew
Note: See TracTickets for help on using tickets.
Back to Top