question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Update different records in table with different values

See original GitHub issue

Problem

Lets consider a user table with columns ID and NAME . If i wish to update multiple Names based on their IDs, for example -

  • For id=1, name should be updated to name=“abc”
  • For id=2, name should be updated to name=“def”
  • For id=3, name should be updated to name=“xyz”

I can’t do that in one single query.

Suggested solution

There should be a way to bulk update records in a table with different values. For example -

prisma.user.updateMany({ where:{ id: { in: [1,2,3] } }, data:{ name:["abc", "def", "xyz"] } })

OR

prisma.user.updateMany({ [ { where:{ id: 1 }, data:{ name:"abc" } }, { where:{ id: 2 }, data:{ name:"def" } }, ] })

Alternatives

It can’t be done using updateMany since it only allows to update all records with one single piece of data. For example - prisma.user.updateMany({ where:{ id: { in: [1,2,3] } }, data:{ name: "abc" } })

Additional context

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:36
  • Comments:7

github_iconTop GitHub Comments

4reactions
binajmencommented, Jan 28, 2022

Would be very inresting – instead of currently using the combo map & update

1reaction
drnachiocommented, Jul 7, 2022

It doesn’t make any sense when you have to update thousands of documents to thousands of different values ​​to perform thousands of updates, if you know from the beginning the information you want to update, so not being able to do this type of operation limits a lot for performance reasons.

We will need to be able to use something like bulkWrite (https://www.mongodb.com/docs/manual/reference/method/db.collection.bulkWrite/)

Read more comments on GitHub >

github_iconTop Results From Across the Web

sql - UPDATE multiple rows with different values in one query ...
$sql = "SELECT COUNT(*) AS _num FROM test; INSERT INTO test(id) VALUES (1); SELECT COUNT(*) AS _num FROM test; "; $mysqli->multi_query($sql);. comparing result ......
Read more >
How to Update a Column Based on a Filter of Another Column
Learn how to update a column based on a filter of another column. This tutorial will cover ways to update rows, including full...
Read more >
Updating multiple rows with different values in one query
You will need to write very complicated conditions if you want to update more than two rows. In such a case you can...
Read more >
How to Update Multiple Records Using One Query in SQL ...
Use the keyword UPDATE and WHERE to achieve this. Syntax: UPDATE TABLE_NAME SET COLUMN_NAME = VALUE WHERE CONDITION; Query:
Read more >
Update multiple rows in SQL with different values at once
Update multiple rows in SQL with different values at once · knex.transaction(function(trx) { · UPDATE users · SELECT 1 as id, 'myFirstName1' as...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found