Skip to content

Conversation

tim-schilling
Copy link
Member

Description

The bulk_create queryset method allows users to utilize database's upsert functionality. Or at least something close to it. This PR seeks to support that functionality.

Some changes:

  • When update_conflicts is specified, a second query has to run to fetch the updated data. It's possible the model instances in memory do not reflect the values in the database. This is tested in test_bulk_create_history_with_duplicates_update_conflicts_create_only_field.
  • The history_type is attempted to be calculated based on whether there is an existing historical record for an instance rather than assuming it's always being created. This is done via an annotation and an Exists subquery.

Related Issue

Fixes #1323

Motivation and Context

See description.

How Has This Been Tested?

I tested this with postgres and sqlite, we'll see what CI says 🤞

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • I have run the pre-commit run command to format and lint.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • I have added my name and/or github handle to AUTHORS.rst
  • I have added my change to CHANGES.rst
  • All new and existing tests passed.

@tim-schilling tim-schilling closed this by deleting the head repository Jun 11, 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.

"update_fields" option to "bulk_create" not supported?

1 participant