prisma: Implement `RETURNING` on `updateMany()` so it can return data (instead of just count)

Hi there. Currently methods like updateOne and findOne takes only unique attributes in the where clause. The problem is, you need not always have a unique/primary key on a record which you want to find.

Its inconvenient since I have to use methods like updateMany when doing this which does not return me the updated records, but just the count. Also due to the fact that composite primary keys are not implemented yet (https://github.com/prisma/photonjs/issues/339) its kind of inconvenient since a single column is many times not unique.

Right now, when using updateMany, I have to do updateMany to first update the records and then a findMany query to get the records leading to 2 queries since when doing updates, you often don’t receive all inputs from users but only specific fields they want to update, but you want all the results as the response post the update.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 175
  • Comments: 38 (5 by maintainers)

Most upvoted comments

Can we please stick to using the lovely “👍” reaction on the top post instead of posting +1 ? This will be more respectful of those that are subscribed to these issue and wish to be aware of any useful update.

Thank you

Hi. I need this RETURNING option too.

This would be a very nice to have.

Instead of a raw query, I’ve opted to use a combination of findMany + updateMany in a transaction.

const query = {...};

const [result] = await prismaClient.$transaction([
        prismaClient.table.findMany({
          select: { id: true }, // only select the id of the record that will be updated
          where: query.where,
        }),
        prismaClient.table.updateMany(query),
])

const ids = result.map(record => record.id);

When using updateMany I need to know which rows were affected, so I can send events about the changes to pub/sub.

Count is not sufficient for me to accomplish this…

This feature is implemented in version 5.14.0

https://github.com/prisma/prisma/releases/tag/5.14.0

I also find this very inconvenient. Why would you constrain return value to only count? If update supports selecting data to be returned after the query, why updateMany is different? Is there any technical reason that I am not aware of? Is this ever going to be fixed?

This feature would be amazing to have.

Any update of this? 😕

Any update of this? 😕

Hi,

Non unique values in findOne is not possible as as they doesn’t guarantee there will be a single record in the database. Non unique fields can refer to more than one row in the database, that is why you have to use findMany or updateMany to update non unique values.

Right now, when using updateMany, I have to do updateMany to first update the records and then a findMany query to get the records leading to 2 queries since when doing updates, you often don’t receive all inputs from users but only specific fields they want to update, but you want all the results as the response post the update.

Maybe we can implement data returning on updates as SQL supports it using the RETURNING keyword, marking this as a FR for now.

This is a very important feature to have

@DanEscudero I fear you misconnected some dots there. That release adds a new createManyAndReturn() Prisma Client query, but does not change the behavior of updateMany() or add any update related functionality.

I really needed this too right now. Would be a nice feature.

until this is developed, we opted for using a transaction as well… but this feature would be very useful

Yeah, it hasn’t been released @janpio - @DanEscudero is incorrectly associating the new createManyAndReturn with this request

Four years and counting? What could be higher priority?

I really need this too. 😦

I need this too.