mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-09-11 11:04:10 +02:00
Option to ignore inserts if primary key exists already
Support for SQLite's INSERT OR IGNORE
In the API layer it looks like this:
fresh_db["test"].insert({"id": 1, "bar": 3}, ignore=True)
For the CLI layer it looks like this:
$ sqlite-utils insert data.db dogs dogs.json --ignore
Closes #21
This commit is contained in:
parent
1e28eeee8c
commit
00c5a49a87
4 changed files with 66 additions and 7 deletions
|
|
@ -392,6 +392,18 @@ def test_insert_thousands_ignores_extra_columns_after_first_100(fresh_db):
|
|||
assert [{"i": 101, "word": None}] == rows
|
||||
|
||||
|
||||
def test_insert_ignore(fresh_db):
|
||||
fresh_db["test"].insert({"id": 1, "bar": 2}, pk="id")
|
||||
# Should raise an error if we try this again
|
||||
with pytest.raises(sqlite3.IntegrityError):
|
||||
fresh_db["test"].insert({"id": 1, "bar": 2}, pk="id")
|
||||
# Using ignore=True should cause our insert to be silently ignored
|
||||
fresh_db["test"].insert({"id": 1, "bar": 3}, pk="id", ignore=True)
|
||||
# Only one row, and it should be bar=2, not bar=3
|
||||
rows = fresh_db.execute_returning_dicts("select * from test")
|
||||
assert [{"id": 1, "bar": 2}] == rows
|
||||
|
||||
|
||||
def test_insert_hash_id(fresh_db):
|
||||
dogs = fresh_db["dogs"]
|
||||
id = dogs.upsert({"name": "Cleo", "twitter": "cleopaws"}, hash_id="id").last_pk
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue