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)
[x] New feature (non-breaking change which adds functionality)
[ ] Breaking change (fix or feature that would cause existing functionality to change)
Checklist:
[x] I have run the pre-commit run command to format and lint.
[x] My change requires a change to the documentation.
[x] I have updated the documentation accordingly.
[x] I have read the CONTRIBUTING document.
[x] I have added tests to cover my changes.
[x] I have added my name and/or github handle to AUTHORS.rst
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:
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 intest_bulk_create_history_with_duplicates_update_conflicts_create_only_field
.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 anExists
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
Checklist:
pre-commit run
command to format and lint.AUTHORS.rst
CHANGES.rst