Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated bulk_create_with_history to support update_conflicts of bulk_create #1419

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

tim-schilling
Copy link
Contributor

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 force-pushed the bulk-create-with-update-conflicts branch from 1286a72 to 2594da5 Compare November 19, 2024 21:13
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