Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prisma: Update using "where" with non-unique fields?

When checking out the typings for the update method in Prisma ORM, I found that I can only include a where clause for unique fields:

return this.prisma.publication.update({
    data,
    where: {id},
});

I'd love to combine this with authentication and do something like:

return this.prisma.publication.update({
    data,
    where: {id, owner: user.id}, <-- Authorisation
});

Is this at all possible with Prisma or do I need a separate query for this that retrieves the instance?

like image 347
Xen_mar Avatar asked Mar 07 '26 18:03

Xen_mar


2 Answers

In this case, you would need to use updateMany as you are using a non-unique field along with the unique one.

like image 133
Ryan Avatar answered Mar 10 '26 07:03

Ryan


Apparently, as of Prisma version 4.5.0, there is a new feature allowing you to add non-unique fields to the unique where clause as long it contains at least one unique field.

Prisma realese 4.5.0

It's a preview feature so in order to enable it, add this to your shcema.prisma:

generator js {
  provider        = "prisma-client-js"
  previewFeatures = ["extendedWhereUnique"]
}

And then your code sample will work.

return this.prisma.publication.update({
  data,
  where: {id, owner: user.id},
});
like image 41
DavidCohen Avatar answered Mar 10 '26 06:03

DavidCohen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!