Skip to content

Conversation

@BobdenOs
Copy link
Contributor

@BobdenOs BobdenOs commented Dec 22, 2025

It is possible to leave out key columns which have a default or cds.on.insert value when calling UPSERT. This case was not properly considered when using UPSERT from SELECT queries. This PR ensures to include all key columns into the source SELECT query.

behavior

In the case of HANA the result of the source SELECT is not allowed to have duplicate key values. As for postgres and sqlite they leverage the emergent behavior of an UPSERT that the rows are allowed to overwrite each other. This is actually an undefined behavior for postgres and sqlite.

As example book 1 is being updated inside a single UPSERT statement twice. There is no guarantee that title 2 will be the final result. It is just as valid for title 1 to be the final result. To prevent these edge cases HANA will throw an error that ID is violating an unique constraint even before applying the UPSERT behavior which invalidates the unique constraint restriction.

UPSERT INTO Books (
  SELECT 1 as ID, 'title 1' as title
UNION ALL
  SELECT 1 as ID, 'title 2' as title
)

@BobdenOs
Copy link
Contributor Author

BobdenOs commented Dec 22, 2025

  • add reproduction scenario

@BobdenOs BobdenOs enabled auto-merge (squash) December 23, 2025 12:32
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