[Django] #28821: Allow bulk_create on multi-table inheritance when possible

118 views
Skip to first unread message

Django

unread,
Nov 20, 2017, 12:36:05 PM11/20/17
to django-...@googlegroups.com
#28821: Allow bulk_create on multi-table inheritance when possible
-------------------------------------+-------------------------------------
Reporter: Joey | Owner: nobody
Wilhelm |
Type: New | Status: new
feature |
Component: Database | Version: master
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
According to
[https://github.com/django/django/blob/a2626cb3fec2e67bb0ec4be2e992e1a836b7567e/django/db/models/query.py#L423
this comment] in `bulk_create`:

{{{#!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.

Django

unread,
Nov 20, 2017, 2:44:52 PM11/20/17
to django-...@googlegroups.com
#28821: Allow QuerySet.bulk_create() on multi-table inheritance when possible
-------------------------------------+-------------------------------------
Reporter: Joey Wilhelm | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/28821#comment:1>

Django

unread,
Nov 27, 2017, 11:48:07 AM11/27/17
to django-...@googlegroups.com
#28821: Allow QuerySet.bulk_create() on multi-table inheritance when possible
-------------------------------------+-------------------------------------
Reporter: Joey Wilhelm | Owner: Abhishek
| Gautam
Type: New feature | Status: assigned

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Abhishek Gautam):

* status: new => assigned
* cc: Abhishek Gautam (added)
* owner: nobody => Abhishek Gautam


--
Ticket URL: <https://code.djangoproject.com/ticket/28821#comment:2>

Django

unread,
Jan 14, 2018, 10:59:32 AM1/14/18
to django-...@googlegroups.com
#28821: Allow QuerySet.bulk_create() on multi-table inheritance when possible
-------------------------------------+-------------------------------------
Reporter: Joey Wilhelm | Owner: (none)

Type: New feature | Status: new
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Abhishek Gautam):

* status: assigned => new
* owner: Abhishek Gautam => (none)


--
Ticket URL: <https://code.djangoproject.com/ticket/28821#comment:3>

Django

unread,
Nov 29, 2019, 4:21:07 AM11/29/19
to django-...@googlegroups.com
#28821: Allow QuerySet.bulk_create() on multi-table inheritance when possible
-------------------------------------+-------------------------------------

Reporter: Joey Wilhelm | Owner: (none)
Type: New feature | Status: new
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sardorbek Imomaliev):

* cc: Sardorbek Imomaliev (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/28821#comment:4>

Django

unread,
Nov 2, 2020, 1:55:57 PM11/2/20
to django-...@googlegroups.com
#28821: Allow QuerySet.bulk_create() on multi-table inheritance when possible
-------------------------------------+-------------------------------------

Reporter: Joey Wilhelm | Owner: (none)
Type: New feature | Status: new
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Dec 10, 2020, 7:46:37 PM12/10/20
to django-...@googlegroups.com
#28821: Allow QuerySet.bulk_create() on multi-table inheritance when possible
-------------------------------------+-------------------------------------

Reporter: Joey Wilhelm | Owner: (none)
Type: New feature | Status: new
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jon Dufresne):

* 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>

Django

unread,
Dec 11, 2020, 3:39:14 AM12/11/20
to django-...@googlegroups.com
#28821: Allow QuerySet.bulk_create() on multi-table inheritance when possible
-------------------------------------+-------------------------------------
Reporter: Joey Wilhelm | Owner: Jon
| Dufresne

Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* owner: (none) => Jon Dufresne


* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/28821#comment:7>

Django

unread,
Dec 18, 2020, 3:18:07 AM12/18/20
to django-...@googlegroups.com
#28821: Allow QuerySet.bulk_create() on multi-table inheritance when possible
-------------------------------------+-------------------------------------

Reporter: Joey Wilhelm | Owner: Jon
| Dufresne
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* needs_better_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/28821#comment:8>

Django

unread,
Oct 19, 2021, 8:06:42 AM10/19/21
to django-...@googlegroups.com
#28821: Allow QuerySet.bulk_create() on multi-table inheritance when possible
-------------------------------------+-------------------------------------

Reporter: Joey Wilhelm | Owner: Jon
| Dufresne
Type: New feature | Status: assigned
Component: Database layer | Version: dev

(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Shai Berger):

* 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>

Django

unread,
Oct 23, 2021, 6:12:56 AM10/23/21
to django-...@googlegroups.com
#28821: Allow QuerySet.bulk_create() on multi-table inheritance when possible
-------------------------------------+-------------------------------------

Reporter: Joey Wilhelm | Owner: Jon
| Dufresne
Type: New feature | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Adam Johnson):

* cc: Adam Johnson (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/28821#comment:10>

Django

unread,
Jul 22, 2022, 4:34:27 AM7/22/22
to django-...@googlegroups.com
#28821: Allow QuerySet.bulk_create() on multi-table inheritance when possible
-------------------------------------+-------------------------------------

Reporter: Joey Wilhelm | Owner: (none)
Type: New feature | Status: new
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* owner: Jon Dufresne => (none)


* status: assigned => new


--
Ticket URL: <https://code.djangoproject.com/ticket/28821#comment:11>

Django

unread,
Dec 25, 2023, 3:38:14 PM12/25/23
to django-...@googlegroups.com
#28821: Allow QuerySet.bulk_create() on multi-table inheritance when possible
-------------------------------------+-------------------------------------
Reporter: Joey Wilhelm | Owner: HAMA
| Barhamou

Type: New feature | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by HAMA Barhamou):

* 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>

Django

unread,
Jan 18, 2024, 1:23:33 PMJan 18
to django-...@googlegroups.com
#28821: Allow QuerySet.bulk_create() on multi-table inheritance when possible
-------------------------------------+-------------------------------------

Reporter: Joey Wilhelm | Owner: HAMA
| Barhamou
Type: New feature | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Jan 18, 2024, 1:33:25 PMJan 18
to django-...@googlegroups.com
#28821: Allow QuerySet.bulk_create() on multi-table inheritance when possible
-------------------------------------+-------------------------------------

Reporter: Joey Wilhelm | Owner: HAMA
| Barhamou
Type: New feature | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by HAMA Barhamou):

* has_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/28821#comment:14>

Django

unread,
Jan 18, 2024, 11:40:46 PMJan 18
to django-...@googlegroups.com
#28821: Allow QuerySet.bulk_create() on multi-table inheritance when possible
-------------------------------------+-------------------------------------

Reporter: Joey Wilhelm | Owner: HAMA
| Barhamou
Type: New feature | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* 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>

Django

unread,
Jun 6, 2024, 11:54:55 AM (8 days ago) Jun 6
to django-...@googlegroups.com
#28821: Allow QuerySet.bulk_create() on multi-table inheritance when possible
-------------------------------------+-------------------------------------
Reporter: Joey Wilhelm | Owner: HAMA
| Barhamou
Type: New feature | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by benhubsch):

I have a use case where I would like to bulk create the child model and
and the primary key / parent key is known in advance. That seems the most
straightforward version to support, would love to have that enabled
--
Ticket URL: <https://code.djangoproject.com/ticket/28821#comment:16>

Django

unread,
Jun 7, 2024, 4:53:55 AM (8 days ago) Jun 7
to django-...@googlegroups.com
#28821: Allow QuerySet.bulk_create() on multi-table inheritance when possible
-------------------------------------+-------------------------------------
Reporter: Joey Wilhelm | Owner: HAMA
| Barhamou
Type: New feature | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Arthur):

* cc: Arthur (added)

--
Ticket URL: <https://code.djangoproject.com/ticket/28821#comment:17>
Reply all
Reply to author
Forward
0 new messages