-
-
Notifications
You must be signed in to change notification settings - Fork 112
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/upsert_all
fails to write row when not_null
is absent and the schema definition includes not_null
#610
Comments
I don't know if this is a clue, but while testing this I noticed that from sqlite_utils import Database
db = Database(":memory:")
db["birds"].create(
{"id": int, "name": str},
pk="id",
not_null={"name"},
)
db["birds"].insert({"id": 1, "name": "flamingo"}, not_null={"wingspan"})
print("insert complete!")
db["birds"].upsert({"id": 2, "name": "goldfinch"}, pk="id", not_null={"wingspan"})
print("upsert complete!") In this case the The only way to make the from sqlite_utils import Database
db = Database(":memory:")
db["birds"].insert({"id": 1, "name": "flamingo"}, not_null={"wingspan"})
print("insert complete!")
db["birds"].upsert({"id": 2, "name": "goldfinch"}, pk="id", not_null={"wingspan"})
print("upsert complete!") at which point I get an error |
A further, potentially interesting observation: this only seems to apply when you're inserting new rows. If your upsert is modifying an existing row, it works without supplying the Another example: from sqlite_utils import Database
db = Database(":memory:")
db["birds"].create(
{"id": int, "name": str, "color": str},
pk="id",
not_null={"name"},
)
db["birds"].insert({"id": 1, "name": "flamingo"})
print(next(db["birds"].rows))
# initial insert
# {'id': 1, 'name': 'flamingo', 'color': None}
db["birds"].upsert({"id": 1, "name": "goldfinch"}, pk="id")
print(next(db["birds"].rows))
# modifying the existing row
# {'id': 1, 'name': 'goldfinch', 'color': None}
db["birds"].upsert({"id": 1, "color": "blue"}, pk="id")
print(next(db["birds"].rows))
# modifying a column which is allowed to be non-null
# {'id': 1, 'name': 'goldfinch', 'color': 'blue'} (That second case is how I spotted it – I was upserting into a table with not-null columns, but modifying a nullable column on an existing row.) |
i can confirm all of the above. upserts do not work on tables where there are not-null columns, but only the inserts do not work - updates work fine. i am not sure if there would be a better fix, but one possible fix would be to query One other thing worth noting - the documentation asserts that the |
I found a bug where calls to
upsert()
andupsert_all()
don't write rows if:not_null
kwarg on the table definition, andnot_null
in theupsert()
callThis doesn't affect
insert()
orinsert_all()
.Repro example
Here's the output (with my comments on the right)
So calling
upsert()
andupsert_all()
without passingnot_null
isn't actually inserting a record.This problem goes away if:
not_null
argument to theupsert()
calls, ornot_null
from the table definitionVersion info
3.12.0
3.36
3.39.5 2022-10-14
Related issues
It seems very likely that this is related to #538, which was also about
upsert()
and thenot_null
flag.The text was updated successfully, but these errors were encountered: