Skip to content

test: cover bulk upsert dropping records when select excludes identity keys - #809

Closed
marcnnn wants to merge 1 commit into
ash-project:mainfrom
marcnnn:regression-test-bulk-upsert-drops-records
Closed

test: cover bulk upsert dropping records when select excludes identity keys#809
marcnnn wants to merge 1 commit into
ash-project:mainfrom
marcnnn:regression-test-bulk-upsert-drops-records

Conversation

@marcnnn

@marcnnn marcnnn commented Jul 29, 2026

Copy link
Copy Markdown

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.

  • I accept the AI Policy, or AI was not used in the creation of this PR.
  • Bug fixes include regression tests
  • Chores
  • Documentation changes
  • Features include unit/acceptance tests
  • Refactoring
  • Update dependencies

…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.
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.
@zachdaniel zachdaniel closed this Aug 11, 2026
@zachdaniel

Copy link
Copy Markdown
Contributor

tests added in a separate PR, thanks!

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.

2 participants