Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upsert new rows with constraints, fixes #514 #515

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions sqlite_utils/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -2779,13 +2779,16 @@ def build_insert_queries_and_params(
self.last_pk = None
for record_values in values:
# TODO: make more efficient:

# The initial insert statement includes all columns, so that upserts
# of new rows whose non-pk columns have constraints can succeed
record = dict(zip(all_columns, record_values))
sql = "INSERT OR IGNORE INTO [{table}]({pks}) VALUES({pk_placeholders});".format(
sql = "INSERT OR IGNORE INTO [{table}]({columns}) VALUES({column_placeholders});".format(
table=self.name,
pks=", ".join(["[{}]".format(p) for p in pks]),
pk_placeholders=", ".join(["?" for p in pks]),
columns=", ".join(["[{}]".format(p) for p in all_columns]),
column_placeholders=", ".join(["?" for p in all_columns]),
)
queries_and_params.append((sql, [record[col] for col in pks]))
queries_and_params.append((sql, [record[col] for col in all_columns]))
# UPDATE [book] SET [name] = 'Programming' WHERE [id] = 1001;
set_cols = [col for col in all_columns if col not in pks]
if set_cols:
Expand Down
14 changes: 14 additions & 0 deletions tests/test_upsert.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ def test_upsert_error_if_no_pk(fresh_db):
table.upsert({"id": 1, "name": "Cleo"})


def test_upsert_with_constraints(fresh_db):
table = fresh_db.create_table(
"table_with_constraints",
{
"id": "text",
"name": "text",
},
not_null=["name"],
)

table.upsert({"id": 1, "name": "Cleo"}, pk="id")
assert 1 == table.last_pk


def test_upsert_with_hash_id(fresh_db):
table = fresh_db["table"]
table.upsert({"foo": "bar"}, hash_id="pk")
Expand Down