simonw / sqlite-utils Public
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
The ".upsert()" method is misnamed #66
Comments
This relates to this bug: dogsheep/github-to-sqlite#8 (comment) |
Fixing this is going to be a real pain. There's lots of code out there that uses Maybe I need to introduce new terms for both of these different patterns and deprecate the existing Or maybe I fix this and ship |
If I do implement the correct definition of INSERT OR IGNORE INTO book(id) VALUES(1001);
UPDATE book SET name = 'Programming' WHERE id = 1001; |
This warrants making a backwards compatible change, which means I'll need to bump the major version number and release 2.0. I'm going to rename the existing |
Is |
Maybe It could be an alias for |
This is going to affect the design of the CLI subcommands as well. |
Maybe instead of inventing a new term I should tell people to use |
First step: add a sqlite-utils/sqlite_utils/db.py Lines 938 to 946 in 8dab9fd
|
Urgh this is going to be quite a bit of work, especially in the CLI module which shares an implementation for |
Thinking about this further: I believe every time I've personally used So I'm happy with I'll still ship it as version 2.0 since it's technically a breaking change. |
Don't forget to update the documentation. This will be quite an involved task. |
I'm going to start by ignoring the existing Then I'll figure out how to implement the new Then I'll update the documentation, and ship |
Last step: update changelog and ship 2.0. Then I can close this issue. |
I shipped 2.0 - release notes here: https://sqlite-utils.readthedocs.io/en/stable/changelog.html#v2 I also wrote about it on my blog: https://simonwillison.net/2019/Dec/30/sqlite-utils-2/ |
This thread here is illuminating: https://stackoverflow.com/questions/3634984/insert-if-not-exists-else-update
The term
UPSERT
in SQLite has a specific meaning as-of 3.24.0 (2018-06-04): https://www.sqlite.org/lang_UPSERT.htmlIt means "behave as an UPDATE or a no-op if the INSERT would violate a uniqueness constraint". The syntax in 3.24.0+ looks like this (confusingly it does not use the term "upsert"):
Here's the problem: the
sqlite-utils
.upsert()
and.upsert_all()
methods don't do this. They use the following SQL:If the record already exists, it will be entirely replaced by a new record - as opposed to updating any specified fields but leaving existing fields as they are (the behaviour of "upsert" in SQLite itself).
The text was updated successfully, but these errors were encountered: