Prisma 2 Documentation on where clause with multiple conditions in update and delete statements #4185
-
I cant seem to find any information on running the following code: return await prisma.users.update({
where: {
user_id: id,
active: true,
},
...data,
}); It wont allow the 2 where clauses, and the examples are simple where most projects aren't. Is it possible to put some more complex examples in the examples repo for myself and others to shift from sql to prisma seamlessly |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments 38 replies
-
We have docs on the supported operations here. Currently you're trying to update on multiple conditions where one of them is not unique i.e. the |
Beta Was this translation helpful? Give feedback.
-
I would like to second this "issue". Technically, if you have a required unique field, like the primary key in my case... adding as optional any other field to filter on should be possible and you will be guaranteed that the result is unique. Without this feature, how else am I suppose to accomplish this mid-air-collision avoidance? By creating a transaction with an otherwise extraneous select in it? |
Beta Was this translation helpful? Give feedback.
-
It would be a tremendous benefit to put multiple conditions inside the where clause of an update and delete statement.
I get the user.id from the session so if someone is trying to update something they don't own, then the update would fail. |
Beta Was this translation helpful? Give feedback.
-
It seems to me that having to do an "extra" SELECT to retrieve the data you may have just updated is not that big of a deal. If you were not using an ORM like Prisma, this is what you'd need to do. In the updateUnique case, is Prisma just doing that SELECT for you? It looks like the confusion here boils down to the fact that one type of UPDATE returns the new data and one just returns counts. Although I agree that the fact that you can't do an updateUnique with a predicate containing multiple conditions that are guaranteed to refer to a unique row is confusing. |
Beta Was this translation helpful? Give feedback.
-
@panzi I was not aware of |
Beta Was this translation helpful? Give feedback.
-
Any idea of when something like this could be supported? |
Beta Was this translation helpful? Give feedback.
-
A related issue: #7290 The reason this probably wont happen any time soon is because the prisma framework generates classes specifically for when we are doing a unique selection, to only expose those properties. This would require deleting all of that work and basically using the same class used by the "Many" calls to expose all of the properties, with the caveat that when doing a select, update or delete... it would iterate through to make sure you chose to filter by at least one property that can determine uniqueness. While significantly more usable, you turn what was once a compile time catch into something that will throw at run time. Honestly, I don't know how Prisma is handling composite indexes with this... it could be a complete mess. All that to say, you're probably better off to write a plugin that exposes this functionality than to expect anyone on the Prisma project to actually redesign everything. This discussion is almost 2 years old already. |
Beta Was this translation helpful? Give feedback.
-
Use
|
Beta Was this translation helpful? Give feedback.
@Dreamystify 👋
We have docs on the supported operations here. Currently you're trying to update on multiple conditions where one of them is not unique i.e. the
active
part.update
just taken in unique values so in this case, you would need to replaceupdate
withupdateMany
.