tortoise / tortoise/tortoise-orm

Bulk Create without returning objects

Open
#1,614 9 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
5.6k
Forks
516
Avg merge
2d 21h
Merged PRs (30d)
9

Description

The main problem here is that bulk_create converts objects to a list before insertion which loads all the models at the same time. The only reason for this is to return the inserted objects after the query is done. However, those objects are not being updated (adding pk for example), i.e the mothod will return what you sent. I think we can enhance this and make bulk_create benefits from #1613 .

Describe the solution you'd like
There are several approaches for this:

  1. Do not return anything from bulk_create or return the number of inserted rows. This maybe a breaking change.
  2. Add a keyword argument called return_objects or something like that to control the behaviour.
  3. Do not return anything when batch_size is set.

If none of the above seems logical, I think at least we should mention what is going behind the scenes in the docs, so the user can handle chunking on his own. And maybe use tuple(objects) instead of list(objects).

Describe alternatives you've considered
The most natural alternative is to chunk data on your side and leave batch_size as None. But this makes batch_size useless.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the bulk_create implementation and the discussion in this issue, then inspect how batch_size currently affects insertion and returned objects. The issue presents several incompatible API options without selecting one, so done requires an agreed behavior that avoids unnecessary eager loading or clearly documents the existing behavior, with corresponding tests or documentation.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
database
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.