GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — The ".upsert()" method is misnamed · Issue #66 · simonw/sqlite-utils

The ".upsert()" method is misnamed #66

New issue
Copy link
New issue
Copy link
Closed
Closed
The ".upsert()" method is misnamed#66
Copy link
Labels
bugSomething isn't workingquestionFurther information is requested

Description

@simonw
simonw
opened on Nov 12, 2019
Issue body actions

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.html

It 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"):

INSERT INTO phonebook(name,phonenumber) VALUES('Alice','704-555-1212')
  ON CONFLICT(name) DO UPDATE SET phonenumber=excluded.phonenumber

Here's the problem: the sqlite-utils .upsert() and .upsert_all() methods don't do this. They use the following SQL:

INSERT OR REPLACE INTO [{table}] ({columns}) VALUES {rows};

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).

Reacted by Kyle King and Daniel Hoherd

Activity

Next
  1. simonw
    added
    bugSomething isn't working
    questionFurther information is requested
    on Nov 12, 2019
  2. simonw commented on Nov 12, 2019

    @simonw
    simonw
    on Nov 12, 2019
    OwnerAuthor
    More actions

    This relates to this bug: dogsheep/github-to-sqlite#8 (comment)

  3. simonw commented on Nov 12, 2019

    @simonw
    simonw
    on Nov 12, 2019
    OwnerAuthor
    More actions

    Fixing this is going to be a real pain. There's lots of code out there that uses sqlite-utils with the expectation that upsert() behaves as it currently does.

    Maybe I need to introduce new terms for both of these different patterns and deprecate the existing .upsert() and .upsert_all() since their behaviour can't be changed?

    Or maybe I fix this and ship sqlite-utils 2.0 with a breaking change?

  4. simonw commented on Nov 12, 2019

    @simonw
    simonw
    on Nov 12, 2019
    OwnerAuthor
    More actions

    If I do implement the correct definition of .upsert() I think I'll use this pattern, since it works in versions of SQLite prior to 3.24:

    INSERT OR IGNORE INTO book(id) VALUES(1001);
    UPDATE book SET name = 'Programming' WHERE id = 1001;
  5. simonw
    changed the title [-]I think ".upsert()" may be mis-named![/-] [+]The ".upsert()" method is mis-named![/+] on Nov 13, 2019
  6. simonw commented on Nov 13, 2019

    @simonw
    simonw
    on Nov 13, 2019
    OwnerAuthor
    More actions

    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 upsert() and upsert_all() methods to replace() and replace_all() - then write new upsert() and upsert_all() methods that implement the correct behavior.

  7. simonw commented on Nov 13, 2019

    @simonw
    simonw
    on Nov 13, 2019
    OwnerAuthor
    More actions

    Is replace() a good name here? It doesn't really convey the idea that a brand new record will be created if there isn't an existing one to replace.

  8. simonw commented on Nov 13, 2019

    @simonw
    simonw
    on Nov 13, 2019
    OwnerAuthor
    More actions

    Maybe inplace() (combining "insert" and "replace")?

    It could be an alias for .insert(..., replace=True)

  9. simonw commented on Nov 13, 2019

    @simonw
    simonw
    on Nov 13, 2019
    OwnerAuthor
    More actions

    This is going to affect the design of the CLI subcommands as well.

  10. simonw commented on Nov 13, 2019

    @simonw
    simonw
    on Nov 13, 2019
    OwnerAuthor
    More actions

    Maybe instead of inventing a new term I should tell people to use .insert(..., replace=True) directly. That matches ignore=True.

  11. simonw commented on Nov 13, 2019

    @simonw
    simonw
    on Nov 13, 2019
    OwnerAuthor
    More actions

    First step: add a replace=True argument to insert() and insert_all() that does the same thing as the current upsert=True

    sqlite-utils/sqlite_utils/db.py

    Lines 938 to 946 in 8dab9fd

    def insert_all(
    self,
    records,
    pk=DEFAULT,
    foreign_keys=DEFAULT,
    column_order=DEFAULT,
    not_null=DEFAULT,
    defaults=DEFAULT,
    upsert=DEFAULT,

  12. simonw
    changed the title [-]The ".upsert()" method is mis-named![/-] [+]The ".upsert()" method is misnamed[/+] on Nov 15, 2019
  13. simonw commented on Nov 15, 2019

    @simonw
    simonw
    on Nov 15, 2019
    OwnerAuthor
    More actions

    Urgh this is going to be quite a bit of work, especially in the CLI module which shares an implementation for upsert and insert in a way that looks like it will have to be unwrapped.

  14. simonw commented on Nov 19, 2019

    @simonw
    simonw
    on Nov 19, 2019
    OwnerAuthor
    More actions

    Thinking about this further: I believe every time I've personally used upsert in the past (either with the Python library or the CLI tool) I've actually wanted the new behaviour, where "upsert" means "update existing record with these changes, or insert a new record if one does not exist".

    So I'm happy with upsert doing that, and insert --replace being added as an option that does what upsert does ta the moment.

    I'll still ship it as version 2.0 since it's technically a breaking change.

  15. 1 remaining item

  16. simonw commented on Dec 27, 2019

    @simonw
    simonw
    on Dec 27, 2019
    OwnerAuthor
    More actions

    I'm going to start by ignoring the existing upsert entirely and implementing .insert(..., replace=True) and $ sqlite-utils insert --replace. Including updating the tests.

    Then I'll figure out how to implement the new .upsert() / $ sqlite-utils upsert.

    Then I'll update the documentation, and ship sqlite-utils 2.0.

  17. simonw
    added 10 commits that reference this issue on Dec 27, 2019

    insert --replace and insert(..., replace=True)

    a8c3d85

    Ran black, plus added comments for next step

    de46f59

    insert --replace and insert(..., replace=True)

    866a5bc

    Ran black, plus added comments for next step

    79cc8b8

    New upsert implementation, refs #66

    4a2244b

    Documentation for new upsert v.s insert-replace

    ab8a4bd

    insert --replace and insert(..., replace=True)

    607a2a9

    Ran black, plus added comments for next step

    cfbc099

    New upsert implementation, refs #66

    84bcabd

    Documentation for new upsert v.s insert-replace

    9f47e8b
  18. simonw commented on Dec 30, 2019

    @simonw
    simonw
    on Dec 30, 2019
    OwnerAuthor
    More actions

    Last step: update changelog and ship 2.0. Then I can close this issue.

  19. simonw
    added a commit that references this issue on Dec 30, 2019

    Updated help for upsert, refs #66

    a0f0175
  20. simonw commented on Dec 31, 2019

    @simonw
    simonw
    on Dec 31, 2019
    OwnerAuthor
    More actions

    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/

  21. simonw
    closed this as completedon Dec 31, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingquestionFurther information is requested

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.