-
-
Notifications
You must be signed in to change notification settings - Fork 115
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
table.upsert_all
fails to write rows when not_null
is present
#538
Comments
Confirmed - I added this test and it fails: def test_upsert_all_not_null(fresh_db):
# https://github.com/simonw/sqlite-utils/issues/538
fresh_db["comments"].upsert_all(
[{"id": 1, "name": "Cleo"}],
pk="id",
not_null=["name"],
)
assert list(fresh_db["comments"].rows) == [{"id": 1, "name": "Cleo"}] |
From time in the debugger, after creating the table it ends up doing this:
|
Here's the problem: import sqlite3
db = sqlite3.connect(":memory:")
db.execute('create table foo (id integer primary key, name not null)')
db.execute('insert into foo (id) values (1)') Produces:
But this: db.execute('insert or ignore into foo (id) values (1)') Completes without an exception. |
Here's the code at fault: sqlite-utils/sqlite_utils/db.py Lines 2774 to 2788 in 80763ed
|
This feels like a fundamental flaw in the way upserts are implemented by One fix would be to switch to using the But...
I still want to support SQLite versions earlier than that. |
I could detect if this happens using This would also require some major re-engineering, since currently it all works by generating a list of SQL queries in advance and applying them inside a loop in sqlite-utils/sqlite_utils/db.py Lines 2839 to 2878 in 80763ed
|
How about if I had logic which checked that all not-null columns were provided in the call to Something like this: [
('INSERT OR IGNORE INTO [comments]([id], name) VALUES(?, ?);', [1, '']),
('UPDATE [comments] SET [name] = ? WHERE [id] = ?', ['Cleo', 1])
] |
That fix seems to work! |
perfect, thank you! |
Hello, I am experiencing a similar issue: Python version: 3.9.6 I create a table as:
Resulting in the schema:
When I try to upsert a dict that looks like However, if I try upserting without |
I found an odd bug today, where calls to
table.upsert_all
don't write rows if you include thenot_null
kwarg.Repro Example
The schema is correctly created:
But no rows are created. Removing either the
not_null
kwargs works as expected, as does aninsert_all
call.Version Info
3.11.0
3.30
3.39.5 2022-10-14
The text was updated successfully, but these errors were encountered: