Repository navigation
Description
Problem
To avoid mid-air-collisions in REST there are two primary techniques: Last-Modified / If-Unmodified-Since, and ETags.
Prisma makes it extremely difficult to support these techniques.
There is no concept of SELECT FOR UPDATE, which is required to ensure that nothing issues an update underneath me while I am verifying the @updatedat value for If-Unmodified-Since, or the hash of the select results if I am using ETags.
Suggested solution
I would recommend a solution that uses a callback.
This is just one idea. It would create a transaction and use a FOR UPDATE on the SELECT if it detects an 'update' or 'delete' property.
My callback is placed in 'update.before', as to not pollute the root property list.
If my callback throws, the transaction is broken and we are done.
If it goes through, we issue the update... or delete.
It could look like this:
// My own custom 'middleware' allowing me to do what ever I'd like before the update.
const myCallback = result => {
if (!result) {
throw new HTTPException(204, 'No Content');
}
if (result.updatedAt > myLastModifiedDate) {
throw new HTTPException(412, 'Record has been modified');
}
};
prisma.user.findUnique({
where: {
id: id
},
select: {
updatedAt: true
},
update: {
before: myCallback,
data: myUpdatedData
}
});Alternatives
There may be a number of ways one could accomplish this.
Activity
swipeby-dev commentedon Oct 20, 2021
I just wanted to note that the above was just an example of what this type of solution could solve.
For solving the specific problem above, mid-air-collision with updatedAt, it would be recommended to blindly apply the update WHERE updatedAt has not changed, and if it fails then select to see if it existed (if you need to know that). This is made more difficult with Prisma due to the lack of filtering by non-unique values even if the where clause is guaranteed to always be unique.
I also wanted to note that .Net's Entity framework does not appear to offer any type of select for update capability.
ziimakc commentedon Mar 20, 2022
I would suggest to pass around a transaction object in "interactive transactions" as argument, like here: https://vincit.github.io/objection.js/guide/transactions.html#using-a-transaction
GuiSim commentedon May 3, 2022
FOR UPDATEis a core tool for achieving atomic guarantees for distributed applications.Here are some issues that have been closed but aren't totally resolved without better
FOR UPDATEmechanism:#1918
#1986
And this issue would also be avoidable with
FOR UPDATEby enabling developers to manually serialize operations#8668
bodinsamuel commentedon Apr 2, 2023
Hey, I will respectfully +1 since it's not been udpated in a long time.
Transactions and Nested Write are great and solve many issues, but locking for update (and all the others keywords https://www.postgresql.org/docs/current/explicit-locking.html) have very specific meaning that are not solved by the proposed solutions right now.
It would be great to have more updates on this since it's a core feature in many databases and makes the switch to Prisma quite harder.
Thinkscape commentedon Jul 18, 2023
I'm having bad time trying to work around this mess with
.executeRawand that not working very well because of how Prisma does things.Transactionality is the prime reason to use RDBMS in the first place, and Prisma gets in the way of that...
Related to #1918
[-]An integrated SELECT FOR UPDATE solution[/-][+]`SELECT FOR UPDATE`[/+]janpio commentedon Mar 1, 2024
Potentially related:
#17136
#5983
JasperSui commentedon May 24, 2024
I really hope this can be done with Prisma instead of using weird workarounds or executing a bunch of raw SQL queries, so here is my +1 for this feature 🙏
rsimonton commentedon Jul 2, 2024
Lobbing another +1, this is potentially a deal breaker for us
2 remaining items
yourconst commentedon Aug 21, 2024
Hello, maintainers!
It is basic functionality.
Why you not implement it?
3 years awaiting is so bad.
Add fast some flag to "select" queries (it may be platform depend flag as temporary solution).
Or give us ETA at least
michaelperros commentedon Aug 21, 2024
I was able to implement SELECT FOR UPDATE in Prisma following the info in this comment: #17136 (comment)
It was pretty straightforward
qisaw commentedon Aug 30, 2024
Would love to not have to write workarounds for this so here's another +1
junajan commentedon Sep 2, 2024
it is a shame that Prisma has no support for this core feature +1
theromanticsoul commentedon Oct 22, 2024
If we take an example of a transaction from the Prisma documentation - example, where Alice and Bob have $ 100 each and quickly trigger a transaction twice, for example by simultaneously clicking on a button from two browser tabs, this will lead to Alice having -100$ and Bob having 300$
+1
rivatove commentedon Nov 3, 2024
SELECT FOR UPDATEis crucial for atomic operations. It's a shame that a tool as mature as Prisma still hasn't implemented it. I'm certain it blocks many enterprise clients from using the library.But the example from the docs should work because it uses an
updatequery.rupin-rk commentedon Dec 7, 2024
@romanticsoul being person 1 and @rivatove being person 2; this is what chatgpt has to say:
The situation described in the Prisma documentation example has potential for race conditions, and person 1 is correct. Here's why:
Analysis of the Example
Transaction Logic Recap:
Potential Issue:
Why does this happen?
Why SELECT ... FOR UPDATE Would Help
Using SELECT ... FOR UPDATE ensures that:
This guarantees that only one transaction can decrement Alice's balance at a time, avoiding the race condition.
Person 2's Argument
Person 2 claims that "the example should work because it uses an update query." However:
Incorrect Assumption: An UPDATE query in itself does not prevent race conditions unless combined with locking.
Correct Point: Prisma does use a single SQL UPDATE statement for decrement operations. However, the atomicity of that statement does not extend to the subsequent logic in the transaction (e.g., checking the balance). This gap allows race conditions.
Solution: Adding SELECT ... FOR UPDATE
To make this example robust, you can:
In Prisma, you would need to use a raw SQL query for this since Prisma currently does not natively support SELECT ... FOR UPDATE:
Conclusion
Prisma's lack of built-in support for SELECT ... FOR UPDATE is indeed a limitation, but workarounds using raw SQL can mitigate this issue.
rupin-rk commentedon Dec 10, 2024
counter to chatGPT:
while this is true for read queries; prisma's example uses update query. if two transaction simultaneously try to update the same row; wouldn't second transaction wait for the first one to finish or rallback even if isolation level is read commited?
response:
You're absolutely correct that UPDATE operations in PostgreSQL inherently acquire row-level locks, even under the READ COMMITTED isolation level. This means the second transaction will wait for the first transaction to complete (commit or rollback) before proceeding. Let’s revisit the Prisma example in light of this.
How UPDATE Queries Handle Concurrent Transactions
In PostgreSQL:
In the Prisma Example
Here’s what happens when two transactions simultaneously execute the transfer function:
Why This Example Works
Because both transactions use UPDATE, PostgreSQL's locking mechanism prevents a race condition. The second transaction cannot modify the row until the first transaction releases its lock, ensuring consistency even at the READ COMMITTED isolation level.
Person 1's Concern
Person 1 argues that this example could allow Alice to end up with a negative balance. However, this can only happen if the balance check (if sender.balance < 0) is not coupled with the UPDATE. In the Prisma example:
Thus, the example is safe as written because:
Person 2's Argument
Person 2 is correct in stating that the use of an UPDATE query ensures row-level locking. This inherent behavior of PostgreSQL makes the example work even without SELECT ... FOR UPDATE.
Summary
Additional Notes
If the example were using SELECT to check the balance before updating (instead of directly decrementing it in the UPDATE), then FOR UPDATE would be required to prevent race conditions. This is likely the confusion in person 1's argument.
rivatove commentedon Jun 15, 2025
@janpio Is there a way to bump the priority of this issue slightly with a bounty? Not talking about anything immediate but maybe to consider it the next time you prepare a roadmap for a quarter?
shoxter commentedon Oct 13, 2025
It's hard to imagine something as critical to serious software as row level read locks is missing from this library. We weren't using it for a week before this was exposed.
If you aren't doing dead simple Todo List type apps, this isn't even optional to go without at any reasonable scale.
coulter-fullgood commentedon Nov 10, 2025
+1
arnauddorgans commentedon Feb 6, 2026
+1
richgarner-nsw-gov commentedon Feb 10, 2026
Found this to be a decent workaround....
ipeterov commentedon Jul 29, 2026
It is slightly insane that it's not in the library - coming from Django ORM, I instinctively reached for it... and it turned out I need to use raw SQL for this basic necessity.