Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save 1mursaleen/95416bf679590debd3e709ab76b42f32 to your computer and use it in GitHub Desktop.
Save 1mursaleen/95416bf679590debd3e709ab76b42f32 to your computer and use it in GitHub Desktop.
AdonisJS Postgres - Multiple Row Update with Different Values in a Single Query
taken from: https://forum.adonisjs.com/t/bulk-update-lucid-model/2505/2
const placeholders = []
const values = []
for (const pickup of pickups) {
placeholders.push('(?,?)')
values.push(pickup.id)
values.push(pickup.city)
}
// Bulk update with individual pickup cities
await Database.raw(`UPDATE pickups AS p SET city = v.city
FROM (VALUES ${placeholders.join(',')})
AS v(id, city) WHERE p.id = v.id`, values)
// You may need to typecast values in the Query, like:
// v.city will become v.city::int4 or v.city::text
// same goes for v.id
// otherwise you may get error in the latest versions of Postgres
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment