How to update multiple rows with one query?

Take this array as an example:
const data = [
{id: 1, col1: "col1 data where id = 1"}
{id: 2, col1: "col1 data where id = 2"}
…(potentially tens of rows)
]
const data = [
{id: 1, col1: "col1 data where id = 1"}
{id: 2, col1: "col1 data where id = 2"}
…(potentially tens of rows)
]
Is it possible to update all the rows with the new data in one query? Using plain sql, something like this will be possible:
WITH data(id, col1) AS (
VALUES
(1, 'col1 data where id = 1'),
(2, 'col1 data where id = 2'),
)
UPDATE my_table
SET col1 = data.col1
FROM data
WHERE my_table.id = data.id;
WITH data(id, col1) AS (
VALUES
(1, 'col1 data where id = 1'),
(2, 'col1 data where id = 2'),
)
UPDATE my_table
SET col1 = data.col1
FROM data
WHERE my_table.id = data.id;
Thanks for your help!
M
Mendy303d ago
Btw, this is something which even Prisma doesn’t support yet, but I’m wondering since drizzle is very different in its approach, maybe there’s a better way of doing this (other then writing a raw sql query)..
L
Lautaro_dapin301d ago
most orms use CASE WHEN for updating you could investigate that
M
Mendy301d ago
Thank you, I will check that out! Although, it’s a bit slow - having to compare each row against all cases. But it’s probably better than making multiple single updates. Btw, here’s a third approach I stumbled upon, which although it works it made me smile:
INSERT INTO mytable (id, a, b, c)
VALUES (1, 'a1', 'b1', 'c1'),
(2, 'a2', 'b2', 'c2'),
(3, 'a3', 'b3', 'c3'),
(4, 'a4', 'b4', 'c4'),
(5, 'a5', 'b5', 'c5'),
(6, 'a6', 'b6', 'c6')
ON DUPLICATE KEY UPDATE id=VALUES(id),
a=VALUES(a),
b=VALUES(b),
c=VALUES(c);
INSERT INTO mytable (id, a, b, c)
VALUES (1, 'a1', 'b1', 'c1'),
(2, 'a2', 'b2', 'c2'),
(3, 'a3', 'b3', 'c3'),
(4, 'a4', 'b4', 'c4'),
(5, 'a5', 'b5', 'c5'),
(6, 'a6', 'b6', 'c6')
ON DUPLICATE KEY UPDATE id=VALUES(id),
a=VALUES(a),
b=VALUES(b),
c=VALUES(c);
Want results from more Discord servers?
Add your server
More Posts
Use Drizzle in NestJS appHi everyone ! I am new to both drizzle and NestJS and was wondering if there was any recipe out ther[Solved] Transforming const to the configured target environment ("es5") is not supported yetSo I had a old project, and I copied the `DATABASE_URL` and on created a basic drizzle project and `Placeholders in inserting: db.insert().values(placeholder('example'))Hey, how do I add placeholders inside the values of an insert prepare statement? I get an typeerror Can I add a Unique constraint to a column?I couldn't find an option for that in the docs. I'm assuming Drizzle doesn't support that. So, coul"Cannot parse date or time" using AWS Data API, aurora postgresI'm trying drizzle-orm (with sst) and aurora pg thru aws data api, when trying to insert a Date valuFiltering against a relationI have a orgs table that have a many to many relation with a users table. while querying with the fGet type for select query?Hey guys, is there a way to infer the "select" type for a given table? for example: ```ts async getorderBy related table columnGiven a relational query such as ``` const matchResult = await db.query.matches.findMany({ Soft Delete StrategyI'm a person of eloquent taste; one who prefers the soft delete. With the current feature set of driColumns that not allowed to updatedHi guys, I'm looking for a proper way to declare SQL schema and its `InferModel` types when there'rExecute sql with '?' as parameterHello everyone. Please, how do I execute a sql that uses as a parameter the character '?' ? For examForeign Key Reference to auth Schema Not Generated in CodeI'm having an issue with my Drizzle-ORM code where the foreign key reference to the 'auth' schema is