Fork me on GitHub
#sql
<
2016-10-31
>
josh.freckleton20:10:30

i use yesql and want to create a SQL fn that updates a subset of fields, but yesql groans if I don't supply all the keys. Do I have to manually provide nil keys?

josh.freckleton20:10:39

UPDATE users SET
  password   = COALESCE(:password, password),
  email      = COALESCE(:email, email),
  last_login = COALESCE(:last_login, last_login)
WHERE id = :id

josh.freckleton20:10:22

notice, this allows me to update just the subset of keys I wish too, and if I, for ex:, don't care to touch the email field, supplying a nil value should leave it untouched