test: cover bulk upsert dropping records when select excludes identity keys - #809
Closed
marcnnn wants to merge 1 commit into
Closed
test: cover bulk upsert dropping records when select excludes identity keys#809marcnnn wants to merge 1 commit into
marcnnn wants to merge 1 commit into
Conversation
…y keys
A bulk upsert pairs each returned row back up with its changeset by the upsert
identity's keys, but the `RETURNING` list is built from the action's
`action_select` alone. When the select doesn't include those keys, every row
reads back with `nil` for each one, nothing correlates, and every record is
dropped from the result - while the rows are still written. The caller sees
`%Ash.BulkResult{status: :success, errors: [], records: []}` for a batch that
inserted or updated fine.
These two tests currently FAIL on main. They are untagged, so they cover both
the `INSERT ... ON CONFLICT` path and the PostgreSQL 17+ `MERGE` path.
`:upsert_with_no_filter` is used because it declares no changes and no
notifiers, which makes its `action_select` just the primary key - an explicit
`select` therefore can't widen it to include the identity key.
1 task
zachdaniel
pushed a commit
that referenced
this pull request
Aug 11, 2026
… bulk upserts don't drop
records (#812)
Bulk upserts correlate each returned row back to its changeset by the upsert
identity keys. Since #786 those keys are no longer read back when the actions
select does not include them (e.g. `upsert_fields []` with `select: []`, as
Ash.Reactors bulk_create step passes), so nothing correlates and every record
is silently dropped from the result while still being written to the database.
Add the identity attribute keys to the upsert RETURNING list so correlation
always succeeds; `Ash.Actions.Helpers.select/2` masks the extra fields back out.
Fix and regression tests originally by @marcnnn (#808, #809).
* fix: include unset (nil) identity keys in bulk upsert correlation
Returning the identity keys (previous commit) is not enough for multi-field
identities that leave a key unset: `Map.take(changeset.attributes, keys)` drops
the unset key, so the changeset correlation key has fewer fields than the
returned rows key and still never matches - the record is silently dropped.
Build the changeset correlation key over every identity key, defaulting unset
ones to nil, so both sides have the same shape. Covers natural-key identities
like [barcode, timestamp, order_id, route_id] with `nils_distinct?: false` where
trailing keys are commonly nil.
Contributor
|
tests added in a separate PR, thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
On upsert the columns that need to be selected to match the updated rows with the rows from the database are not selected anymore so upsert result is empty even if the data is written into the db.
This is a regression that is related to #786 and #638
In my case, an upsert_identity with unique_index defines the upsert semantic already, so the select was left out and is filled by the upsert reactor with the empty list. But the unique index is constructed with two fields not only one like the test, i can add this as well?
My dependabot branch for the ash_postgres update from 2.10.0 to 2.11.0 failed CI
The tests reproduce a simplified version of the problem.
Contributor checklist
Leave anything that you believe does not apply unchecked.