Last active
April 5, 2020 17:27
-
-
Save 1mursaleen/95416bf679590debd3e709ab76b42f32 to your computer and use it in GitHub Desktop.
AdonisJS Postgres - Multiple Row Update with Different Values in a Single Query
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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