Skip to content
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

Cannot set a ON CONFLICT clause on INSERT INTO (SQLite) #448

Closed
lnchan opened this issue Dec 28, 2022 · 7 comments
Closed

Cannot set a ON CONFLICT clause on INSERT INTO (SQLite) #448

lnchan opened this issue Dec 28, 2022 · 7 comments
Assignees
Labels
documentation I need to write something up!

Comments

@lnchan
Copy link

lnchan commented Dec 28, 2022

It seems currently impossible to write the following SQL query using the current HoneySQL helpers:

INSERT OR IGNORE INTO table (column) VALUES ("value");

as defined per SQLite documentation.

SQLite's ON CONFLICT clause is defined here.

@seancorfield
Copy link
Owner

Thanks. There currently isn't a SQLite dialect but I'll probably create one for things that are non-ANSI SQL.

I'll have to look at some of the other databases to see whether these SQLite extensions would be useful in other dialects (and therefore could become core to HoneySQL) or whether they'll need to be part of a new :sqlite dialect.

It will be a while before I can get to this, however.

@seancorfield seancorfield self-assigned this Dec 29, 2022
@seancorfield seancorfield added enhancement needs analysis I need to think about this! labels Dec 29, 2022
@seancorfield
Copy link
Owner

Does SQLite support the (more standard) ON CONFLICT syntax?

INSERT INTO table (column) VALUES ("value") ON CONFLICT (column) DO NOTHING

That syntax is already supported by HoneySQL (because it is what PostgreSQL and others use) so if SQLite supports that, I'd rather not add SQLite-specific extensions for an alternative syntax for the same thing.

@lnchan
Copy link
Author

lnchan commented Jan 15, 2023

That syntax is indeed supported, though it does not seem documented(?) in SQLite documentation. SQLite's own syntax does not require specifying the constraints, but PostgreSQL's ON CONFLICT syntax seems supported and working.

@seancorfield
Copy link
Owner

If you want to provide an example for SQLite, I can add it to the insert into docs, with a note that is how you do insert or ignore...

@seancorfield seancorfield added documentation I need to write something up! and removed enhancement needs analysis I need to think about this! labels Mar 6, 2023
@seancorfield
Copy link
Owner

@lnchan Any thoughts on wording for such an example? I'll close this issue out in another week.

@lnchan
Copy link
Author

lnchan commented Mar 23, 2023

Apologies, I was flying across the ocean, and this slipped my mind.

This is defined as supported per the SQLite documentation on the UPSERT page, meaning that the examples found on the HoneySQL documentation for UPSERTs, such as:

(-> (insert-into :user)
    (values [{:phone "5555555" :name "John"}])
    (on-conflict :phone (where [:<> :phone nil]))
    (do-update-set :phone :name (where [:= :user.active false]))
    (sql/format {:pretty true}))

are properly supported by SQLite since version 3.24.0 (2018-06-04).

However, only a limited subset of this syntax seems to be supported by SQLite. There does not seem to be any support on syntax such as ON CONFLICT ON CONSTRAINT (due to how constraint violations are handled in SQLite); only ON CONFLICT ... DO NOTHING and ON CONFLICT ... DO UPDATE SET seem to be supported by SQLite.

This still means that SQLite's custom ON CONFLICT syntax is not supported, however there are two workarounds (that I know of):

  • for upserts, using MySQL's REPLACE INTO, which is supported by SQLite as an alias to INSERT OR REPLACE INTO;
  • for more precise upserts or insert-or-ignore, PostgreSQL's ON CONFLICT ... DO syntax is supported by SQLite.

Thank you for your time and help (and for your patience! I am really sorry for the delay.)

@seancorfield
Copy link
Owner

Thanks for the detailed updates!

:replace-into is supported in HoneySQL's core as of 2.4.969 (because it's for more than one database and causes no conflicts).

I'll make sure to update the docs in my next "HoneySQL session".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation I need to write something up!
Projects
None yet
Development

No branches or pull requests

2 participants