Skip to content

Fixed #28821 -- Supported multi-table inheritance in bulk_create().#17754

Open
HamaBarhamou wants to merge 6 commits intodjango:mainfrom
HamaBarhamou:feature/bulk-create-mtt-support-28821
Open

Fixed #28821 -- Supported multi-table inheritance in bulk_create().#17754
HamaBarhamou wants to merge 6 commits intodjango:mainfrom
HamaBarhamou:feature/bulk-create-mtt-support-28821

Conversation

@HamaBarhamou
Copy link
Copy Markdown

@HamaBarhamou HamaBarhamou commented Jan 18, 2024

Hi Django Team,

I am picking up where @jdufresne [PR] left off on ticket #28821. My recent commits 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.

Best,
HAMA Barhamou

@HamaBarhamou HamaBarhamou changed the title Support for bulk_create with Multi-Table Inheritance - Django Ticket: #28821 Support for bulk_create with Multi-Table Inheritance Jan 18, 2024
@HamaBarhamou HamaBarhamou force-pushed the feature/bulk-create-mtt-support-28821 branch from d837301 to f4742f7 Compare September 30, 2025 13:03
@jacobtylerwalls
Copy link
Copy Markdown
Member

Hi 👋 @HamaBarhamou. Just strolling by to make sure you know to unset "Patch needs improvement" on the linked ticket when tests are passing and you're ready for a review. Cheers!

@HamaBarhamou
Copy link
Copy Markdown
Author

Salut 👋@HamaBarhamouJe passe juste pour vérifier que vous savez qu'il faut désactiver « Le correctif nécessite des améliorations » sur le ticket lié lorsque les tests sont réussis et que vous êtes prêt pour une révision. À bientôt !

@jacobtylerwalls Thanks for the reminder. I'll do it once I'm ready for the revision.

@HamaBarhamou HamaBarhamou reopened this Oct 1, 2025
@github-actions github-actions bot added the no ticket Based on PR title, no linked Trac ticket label Oct 1, 2025
…- Django Ticket: #28821

This commit introduces the initial implementation for Django ticket #28821, aiming to enable QuerySet.bulk_create to handle models with multi-table inheritance.
Added the _bulk_create_multi_table method to manage parent and child models during bulk insertions, taking into account the database backend's ability to return
IDs after bulk operations.

Note: This is an early-stage implementation and further testing and development are required to ensure functionality and robustness.
…t.bulk_create().

This introduces support for bulk_create() on multi-table inheritance (MTI)
child models in two situations:

* Backends that can return rows from bulk inserts (e.g. PostgreSQL and
  SQLite ≥ 3.35): the top-most parent rows are inserted first and their
  primary keys are propagated through any intermediate MTI levels before
  inserting the child rows.
* Backends without RETURNING: a single MTI level is supported when each
  child instance has its parent pointer pre-set (i.e., child.pk equals
  parent.pk). In practice, bulk-create parents first, then bulk-create
  children with id=parent.pk.

Conflict handling (ignore_conflicts/update_conflicts) is intentionally not
supported for MTI in this initial version and now raises
django.db.utils.NotSupportedError.

The MTI path is factored into an internal _bulk_create_multi_table() helper;
the non-MTI behavior (including conflict options) remains unchanged. Object
state is updated so that instances returned from bulk_create() have
_state.adding = False and _state.db set appropriately.

Tests:
* Single-level and multi-level MTI with RETURNING.
* Preset-parent-pointer fast path (including UUID PKs).
* Mixed preset/non-preset on RETURNING backends.
* Respect for batch_size on the MTI path.
* Explicit parent link attname handling.
* Instance state after insert.
* Negative cases: non-RETURNING without preset pointer; conflict options on MTI.

Docs:
* Expand bulk_create() docs with a “Multi-table inheritance” subsection,
  list supported scenarios and limitations, and clarify behavior.
* Add a note in the 6.1 release notes.

Remove obsolete test asserting that MTI is universally unsupported.
…upport proxy MTI leaves with preset parent pointer.

Detect multi-table inheritance against the concrete model instead of the (possibly proxy) model so proxies don't bypass the MTI path. In the MTI code path, resolve self.model to its concrete_model to avoid inserting parent-only columns into the leaf table.

Also ensure instance state (_state.adding/db) is updated on the preset-parent fast path.

Add tests:
- bulk_create on a proxy over an MTI leaf with preset parent pointer
- bulk_create on a double-proxy over an MTI leaf with preset parent pointer

Verified locally on SQLite, PostgreSQL, and MariaDB.
…double-proxy).

Use parent_link attname and create() parents so PKs are known on non-RETURNING backends.
@HamaBarhamou HamaBarhamou force-pushed the feature/bulk-create-mtt-support-28821 branch from 51b48a9 to 0046141 Compare October 1, 2025 09:39
@HamaBarhamou
Copy link
Copy Markdown
Author

HamaBarhamou commented Oct 1, 2025

ready for revision

@jacobtylerwalls jacobtylerwalls changed the title Support for bulk_create with Multi-Table Inheritance Fixed #28821 -- Supported multi-table inheritance in bulk_create(). Oct 22, 2025
@github-actions github-actions bot removed the no ticket Based on PR title, no linked Trac ticket label Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants