mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-07-23 01:14:31 +02:00
Fix for upsert(hash_id=) bug, closes #84
This commit is contained in:
parent
72fa16b3d9
commit
7c28a4d133
2 changed files with 13 additions and 2 deletions
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue