Skip to content

Conversation

@andyfinnell
Copy link
Contributor

Problem

Apps need the ability to insert or, if the row already exists, update the existing row. This is commonly called "upsert" and is documented for SQLite here. The existing "replace" on conflict clause for insert() often does not have the desired behavior. It will cascade (with deletes, if specified) along foreign keys if the value exists and has to be replaced (see Issue 842).

Solution

Create an upsert() method for queries. The basic form allows for the fallback setters to be manually specified. A slightly more ergonomic version will compute the setters from the inserted values by removing the "conflicting" column, and then using the inserted values via the "excluded" qualifier. Additionally, an Encodable version of upsert is provided.

# Problem

Apps need the ability to insert or, if the row already exists, update the existing row. This is commonly called "upsert" and is [documented for SQLite here](https://www.sqlite.org/lang_UPSERT.html). The existing "replace" on conflict clause for `insert()` often does not have the desired behavior. It will cascade (with deletes, if specified) along foreign keys if the value exists and has to be replaced ([see Issue 842](stephencelis#842)).

# Solution

Create an `upsert()` method for queries. The basic form allows for the fallback setters to be manually specified. A slightly more ergonomic version will compute the setters from the inserted values by removing the "conflicting" column, and then using the inserted values via the "excluded" qualifier. Additionally, an `Encodable` version of `upsert` is provided.
@andyfinnell
Copy link
Contributor Author

andyfinnell commented Feb 24, 2019

Travis is failing on the macOS builds because it is using High Sierra instead of Mojave. UPSERT syntax was added to SQLite with version 3.24.0 (2018-06-04). However, macOS apparently didn't include that version until Mojave, and iOS until 12.0. It might be possible to use @available() to restrict the upsert function to those versions. However, it would mean Travis couldn't run the associated tests.

@nferruzzi
Copy link

nferruzzi commented May 5, 2020

since the main repo looks dead, did you by any chance keep an updated fork with this patch?

@andyfinnell
Copy link
Contributor Author

I don't, sorry.

@nferruzzi
Copy link

@andyfinnell at the end I found another fork with this PR and others merged, so I switched there; thank you for your work :)

https://github.com/thomasrzhao/SQLite.swift

sergeymild added a commit to sergeymild/SQLite.swift that referenced this pull request Jun 9, 2020
@nathanfallet nathanfallet merged commit 4ea6f4e into stephencelis:master Aug 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants