upsert() detects existing compound primary keys

Closes #629
This commit is contained in:
Simon Willison 2026-06-24 10:43:53 -07:00
commit bfd74a35bb
2 changed files with 52 additions and 0 deletions

View file

@ -3554,6 +3554,11 @@ class Table(Queryable):
if hash_id_columns and hash_id is None:
hash_id = "id"
if upsert and not pk and not hash_id and self.exists():
existing_pks = [column.name for column in self.columns if column.is_pk]
if existing_pks:
pk = existing_pks[0] if len(existing_pks) == 1 else tuple(existing_pks)
if upsert and (not pk and not hash_id):
raise PrimaryKeyRequired("upsert() requires a pk")