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

onConflictUpdateSetWhere has wrong type? #644

Open
thomasjm opened this issue Nov 7, 2022 · 3 comments
Open

onConflictUpdateSetWhere has wrong type? #644

thomasjm opened this issue Nov 7, 2022 · 3 comments

Comments

@thomasjm
Copy link
Contributor

thomasjm commented Nov 7, 2022

I noticed that this function's type is

onConflictUpdateSetWhere :: Beamable table
  => (forall s. table (QField s) -> table (QExpr be s) -> QAssignment be s)
  -> (forall s. table (QField s) -> table (QExpr be s) -> QExpr be s Bool)
  -> SqlConflictAction be table

The first callback controls the assignments for the ON CONFLICT UPDATE clause, while the second callback controls the WHERE. It has two arguments,

  • table (QField s) -- this represents the existing table
  • table (QExpr be s) -- this represents the "excluded" row, i.e. the one that would have been inserted if there were no conflict.

I'm confused about the first argument. Shouldn't it be a table (QExpr be s) instead of table (QField s)? If the purpose is to use it to write a QExpr be s Bool, then it seems wrong for it to have this QField type (unless there's a conversion function I'm missing).

I'm also comparing it with functions like conflictingFieldsWhere, which also use the table (QExpr be s) type.

Please let me know if I'm missing something obvious :)

@thomasjm
Copy link
Contributor Author

thomasjm commented Nov 7, 2022

I tried fixing it and the new version worked for me, just submitted a PR :)

@kmicklas
Copy link
Member

kmicklas commented Nov 7, 2022

The QField lets you update the field with <-.. This only applies to the assignment, so we could in theory use QExpr for the condition parameter. I believe both are QField only for consistency, but maybe this was just confusing because it's then inconsistent with the other functions which don't have assignment.

The conversion function you're looking for is current_.

I'll leave this open to track adding better documentation for this.

@kmicklas
Copy link
Member

kmicklas commented Nov 7, 2022

Ah my bad, I see from your PR that you already understood how to use the assignment. Although I think you're right that it would be better to have QExpr for the condition, I'm not sure that it's worth breaking backwards compatibility over it. We should however document the use of current_.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants