mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-09-10 10:34:10 +02:00
.upsert() and upsert_all() require pk=, closes #73
This commit is contained in:
parent
1f3f902ea4
commit
489eda92bc
2 changed files with 18 additions and 3 deletions
|
|
@ -90,6 +90,10 @@ class NotFoundError(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class PrimaryKeyRequired(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class Database:
|
class Database:
|
||||||
def __init__(self, filename_or_conn=None, memory=False):
|
def __init__(self, filename_or_conn=None, memory=False):
|
||||||
assert (filename_or_conn is not None and not memory) or (
|
assert (filename_or_conn is not None and not memory) or (
|
||||||
|
|
@ -957,6 +961,8 @@ class Table(Queryable):
|
||||||
data
|
data
|
||||||
"""
|
"""
|
||||||
pk = self.value_or_default("pk", pk)
|
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)
|
foreign_keys = self.value_or_default("foreign_keys", foreign_keys)
|
||||||
column_order = self.value_or_default("column_order", column_order)
|
column_order = self.value_or_default("column_order", column_order)
|
||||||
not_null = self.value_or_default("not_null", not_null)
|
not_null = self.value_or_default("not_null", not_null)
|
||||||
|
|
@ -1142,9 +1148,6 @@ class Table(Queryable):
|
||||||
alter=DEFAULT,
|
alter=DEFAULT,
|
||||||
extracts=DEFAULT,
|
extracts=DEFAULT,
|
||||||
):
|
):
|
||||||
# Perform the following for each record:
|
|
||||||
# INSERT OR IGNORE INTO books(id) VALUES(1001);
|
|
||||||
# UPDATE books SET name = 'Programming' WHERE id = 1001;
|
|
||||||
return self.insert_all(
|
return self.insert_all(
|
||||||
records,
|
records,
|
||||||
pk=pk,
|
pk=pk,
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,7 @@
|
||||||
|
from sqlite_utils.db import PrimaryKeyRequired
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
def test_upsert(fresh_db):
|
def test_upsert(fresh_db):
|
||||||
table = fresh_db["table"]
|
table = fresh_db["table"]
|
||||||
table.insert({"id": 1, "name": "Cleo"}, pk="id")
|
table.insert({"id": 1, "name": "Cleo"}, pk="id")
|
||||||
|
|
@ -16,6 +20,14 @@ def test_upsert_all(fresh_db):
|
||||||
assert 2 == table.last_pk
|
assert 2 == table.last_pk
|
||||||
|
|
||||||
|
|
||||||
|
def test_upsert_error_if_no_pk(fresh_db):
|
||||||
|
table = fresh_db["table"]
|
||||||
|
with pytest.raises(PrimaryKeyRequired):
|
||||||
|
table.upsert_all([{"id": 1, "name": "Cleo"}])
|
||||||
|
with pytest.raises(PrimaryKeyRequired):
|
||||||
|
table.upsert({"id": 1, "name": "Cleo"})
|
||||||
|
|
||||||
|
|
||||||
def test_upsert_compound_primary_key(fresh_db):
|
def test_upsert_compound_primary_key(fresh_db):
|
||||||
table = fresh_db["table"]
|
table = fresh_db["table"]
|
||||||
table.upsert_all(
|
table.upsert_all(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue