You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
update users as u set-- postgres FTW
email =u2.email,
first_name =u2.first_name,
last_name =u2.last_namefrom (values
(1, 'hollis@weimann.biz', 'Hollis', 'Connell'),
(2, 'robert@duncan.info', 'Robert', 'Duncan')
) as u2(id, email, first_name, last_name)
whereu2.id=u.id;
I tried to replicate this in HoneySQL and got pretty close, but wanted to check:
If there is a better way to write this
If this should be documented/tested somewhere
(sql/format {:update [:user:u]
:set {:email:u2.email:first_name:u2.first_name:last_name:u2.last_name}
:from [[{:values [[1"hollis@weimann.biz""Hollis""Connell"]
[2"robert@duncan.info""Robert""Duncan"]]}
[[:u2:id:email:first_name:last_name]]]]
:where [:=:u2.id:u.id]})
=>
["UPDATE user u SET email = u2.email, first_name = u2.first_name, last_name = u2.last_name FROM (VALUES (?, ?, ?, ?), (?, ?, ?, ?)) AS U2(id, email, first_name, last_name) WHERE u2.id = u.id"1"hollis@weimann.biz""Hollis""Connell"2"robert@duncan.info""Robert""Duncan"]
The main thing I'm concerned about is [[:u2 :id :email :first_name :last_name]]. I think I'm abusing the function call syntax to make a "table call"(?). This seems like it should work fine because SQL is case insensitive, but also feels a bit wrong.
See #527 -- there's an example there, and the docs have been updated to refer to it (in the repo, not in a release yet). I probably need to add examples or at least cross-links from either update or from where it talks about values.
Postgres has a way to run multiple UPDATEs in a single statement with different SET values: https://stackoverflow.com/a/26715934, https://www.postgresql.org/docs/current/sql-update.html
I tried to replicate this in HoneySQL and got pretty close, but wanted to check:
The main thing I'm concerned about is
[[:u2 :id :email :first_name :last_name]]
. I think I'm abusing the function call syntax to make a "table call"(?). This seems like it should work fine because SQL is case insensitive, but also feels a bit wrong.The text was updated successfully, but these errors were encountered: