Repository navigation
Open
Labels
kind/featureA request for a new feature.topic: database-functionalitytopic: findFirst()https://www.prisma.io/docs/orm/reference/prisma-client-reference#findfirsttopic: findMany()topic: findUnique()topic: lockingtopic: transaction
Description
Problem
I just migrated from Sequelize and was quite a fan of Prisma for being type safe. Would Prisma be able to support SELECT FOR UPDATE row locking like Sequelize supports? Understand that Optimistic locking is preferred by Prisma but it would be great if we have the row locking mechanism too.
My problem: When multiple requests call this function concurrently, while first transaction is still processing, the subsequent transaction will see that the user is still active and do the processing again. If we could avoid it by locking the row, that would be great.
function deactivate() {
const user = await prisma.$transaction(async tx => {
const user = await tx.user.findUnique({ where: { id: user.id } });
if (user.status !== 'ACTIVE') {
throw new Error('User not active ...');
}
// some business processing (not long transaction)
return tx.user.update({ where: { id: user.id } }, data: { status: 'DELETED' });
});
await doAfterCommitProcessing(user);
}
Suggested solution
const user = await tx.user.findUnique({ where: { id: user.id }, lock: FOR_UPDATE });
So, the user record with specific id is locked till the transaction is completed.
Alternatives
Additional context
- https://www.cockroachlabs.com/blog/select-for-update/
- https://www.cockroachlabs.com/docs/stable/select-for-update.html
- https://www.postgresql.org/docs/current/sql-select.html#SQL-FOR-UPDATE-SHARE
- https://dev.mysql.com/doc/refman/8.0/en/innodb-locking-reads.html
Reacted by Ennabah, Jonathan Romano, sibeliuz, Karol Ścibior, Vladimir Prudnikov, Aashiq Dheeraj, Sam Lewis, Otto Ling, Andrew Hollenbach, clemwo and 61 more
Metadata
Metadata
Assignees
Labels
kind/featureA request for a new feature.topic: database-functionalitytopic: findFirst()https://www.prisma.io/docs/orm/reference/prisma-client-reference#findfirsttopic: findMany()topic: findUnique()topic: lockingtopic: transaction
Activity
gomain commentedon Jan 23, 2023
Yes, I too have a use case where I want racing reads to block.
luxaritas commentedon Mar 8, 2023
Is this a duplicate of #5983?
janpio commentedon Mar 10, 2023
Yep, sounds similar but a bit ore specific maybe. Will clean up when we look at the issue in depth.
mweel1 commentedon Mar 16, 2023
Same here.
Getting this error when two threads are inserting the same user, the read should to see if the user exists if another transaction is working on it.
gomain commentedon Mar 16, 2023
The current work-around is to either use
$rawQueryto placeFOR UPDATE. Or artificially place a mutex field (typically a boolean) and select via updating the field to true (lock it) where it is false (not locked).This is important if the activities before updating the row involves other side effects that we definitely don't want to happen if the transaction would fail.
KarolScibior commentedon Mar 27, 2023
Any news / plans regarding this feature?
AlejandroFrias commentedon Jun 21, 2023
Another friendly ping. I have that exact user create error. But also am trying to avoid accruing interest on a loan multiple times in the same day on a background job that might retry randomly.
[-]Row locking support in Find queries[/-][+]Row locking support in `find*` queries[/+]1 remaining item
[-]Row locking support in `find*` queries[/-][+]Row locking support in `find*` queries (via `FOR UPDATE`)[/+]esmaeilzadeh commentedon Oct 31, 2023
Is there any plan to add "lock for update" feature to Prisma transactions?
devSajan commentedon Jan 11, 2024
yes, I have the same requirement, when deleting a record another request can access it which should not happen.
[-]Row locking support in `find*` queries (via `FOR UPDATE`)[/-][+]Row locking support in `find*()` queries (via `FOR UPDATE`)[/+]janpio commentedon Mar 1, 2024
Potentially related:
#8580
#5983
easdkr commentedon Apr 2, 2024
Is there any plan to add "lock" (shared, exclusive, update... etc lock) to
findXXXmethod?crgold commentedon Jul 6, 2024
this would be lovely
naylinnpkv commentedon Jul 31, 2024
Has somebody found a work around for that while this feature is not supported yet in Prisma?
homoky commentedon Jul 31, 2024
From other issues I was part of, the roadmap of Prisma is not open, and we can dream about this for years, like people are dreaming for ears about enums in litesql.
alexey-sh commentedon Aug 6, 2024
@naylinnpkv
bilmatech commentedon Feb 24, 2025
This will be a perfect work around.
dimatakoy commentedon Dec 11, 2025
I still need that feature. It's pretty common in my codebases to use
SELECT FOR UPDATE(or, more specifically in PostgreSQL,SELECT FOR NO KEY UPDATE).The workaround is pretty painful for me because our app uses
@map("snake_case_name")for better interoperability with other legacy systems.