Fix for upsert(hash_id=) bug, closes #84

This commit is contained in:
Simon Willison 2020-02-06 23:17:06 -08:00
commit 7c28a4d133
2 changed files with 13 additions and 2 deletions

View file

@ -943,8 +943,6 @@ class Table(Queryable):
data
"""
pk = self.value_or_default("pk", pk)
if upsert and not pk:
raise PrimaryKeyRequired("upsert() requires a pk")
foreign_keys = self.value_or_default("foreign_keys", foreign_keys)
column_order = self.value_or_default("column_order", column_order)
not_null = self.value_or_default("not_null", not_null)
@ -957,7 +955,12 @@ class Table(Queryable):
extracts = self.value_or_default("extracts", extracts)
conversions = self.value_or_default("conversions", conversions)
if upsert and (not pk and not hash_id):
raise PrimaryKeyRequired("upsert() requires a pk")
assert not (hash_id and pk), "Use either pk= or hash_id="
if hash_id:
pk = hash_id
assert not (
ignore and replace
), "Use either ignore=True or replace=True, not both"

View file

@ -28,6 +28,14 @@ def test_upsert_error_if_no_pk(fresh_db):
table.upsert({"id": 1, "name": "Cleo"})
def test_upsert_with_hash_id(fresh_db):
table = fresh_db["table"]
table.upsert({"foo": "bar"}, hash_id="pk")
assert [{"pk": "a5e744d0164540d33b1d7ea616c28f2fa97e754a", "foo": "bar"}] == list(
table.rows
)
def test_upsert_compound_primary_key(fresh_db):
table = fresh_db["table"]
table.upsert_all(