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

It is not possible to use UPSERT with a composite/multicolumn unique index #1219

Open
DominikPalo opened this issue Jul 12, 2023 · 0 comments

Comments

@DominikPalo
Copy link

DominikPalo commented Jul 12, 2023

Build Information

  • SQLite.swift 0.14.1
  • Xcode 14.3.1 (macOS 13.4.1)
  • Swift Package Manager

Issue

I'm trying to execute UPSERT on a table, where the unique index is a composite index - composed of multiple columns (node_id and name in my case). Unfortunately, it seems that the upsert method doesn't allow passing multiple expressions/columns as an onConflictOf argument.

A possible solution would be to allow passing an array instead of just a single expression, e.g.:

// ...
let expNodeId = Expression<Int64>("node_id")
let expName = Expression<String>("name")
let expValue = Expression<String>("value")

myTable.upsert(expValue <- "foo", onConflictOf: [expNodeId, expName])

which would produce a valid UPSERT expression:

INSERT INTO "myTable" ("value") VALUES ('foo') ON CONFLICT ("node_id" , "name") DO UPDATE SET "node_id" = "excluded"."node_id", "name" = "excluded"."name", "value" = "excluded"."value"

UPDATE:
As a workaround, I ended up with this:

myTable.upsert(expValue <- "foo",  Expression<Void>(literal: "\"node_id\", \"name\""))
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

No branches or pull requests

1 participant