{{{#!python
# 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
[https://github.com/django/django/blob/a2626cb3fec2e67bb0ec4be2e992e1a836b7567e/django/db/models/query.py#L448
fails on multi-table inheritance in all cases].
--
Ticket URL: <https://code.djangoproject.com/ticket/28821>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* stage: Unreviewed => Accepted
--
Ticket URL: <https://code.djangoproject.com/ticket/28821#comment:1>
* status: new => assigned
* cc: Abhishek Gautam (added)
* owner: nobody => Abhishek Gautam
--
Ticket URL: <https://code.djangoproject.com/ticket/28821#comment:2>
* status: assigned => new
* owner: Abhishek Gautam => (none)
--
Ticket URL: <https://code.djangoproject.com/ticket/28821#comment:3>
* cc: Sardorbek Imomaliev (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/28821#comment:4>
Comment (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.
--
Ticket URL: <https://code.djangoproject.com/ticket/28821#comment:5>
* cc: jon.dufresne@… (added)
* has_patch: 0 => 1
Comment:
https://github.com/django/django/pull/13763
--
Ticket URL: <https://code.djangoproject.com/ticket/28821#comment:6>
* owner: (none) => Jon Dufresne
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/28821#comment:7>
* needs_better_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/28821#comment:8>
* cc: Shai Berger (added)
Comment:
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)
--
Ticket URL: <https://code.djangoproject.com/ticket/28821#comment:9>
* cc: Adam Johnson (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/28821#comment:10>
* owner: Jon Dufresne => (none)
* status: assigned => new
--
Ticket URL: <https://code.djangoproject.com/ticket/28821#comment:11>
* owner: (none) => HAMA Barhamou
* needs_better_patch: 1 => 0
* has_patch: 1 => 0
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/28821#comment:12>
Comment (by HAMA Barhamou):
Hi Django Team,
I am picking up where @jdufresne left off . My recent commits
([https://github.com/django/django/pull/17754]) introduce the initial
steps towards enabling QuerySet.bulk_create to support multi-table
inheritance.
This is just the beginning, and I plan to make iterative improvements to
this feature. Looking forward to your feedback and suggestions as we
progress.
--
Ticket URL: <https://code.djangoproject.com/ticket/28821#comment:13>
* has_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/28821#comment:14>
* needs_better_patch: 0 => 1
Comment:
Marking as "needs improvement" as author mentioned that it's a draft.
--
Ticket URL: <https://code.djangoproject.com/ticket/28821#comment:15>