Record has changed since last read in table

{ code: 1020, message: "Record has changed since last read in table 'daemon'", state: "HY000" } I get this error once every couple of seconds. I don't really understand why. sure, it might be changed since I read it but I don't care? I'm not sure how to fix
@OnEvent('heartbeat')
async handleHeartbeatEvent(publicKey: string) {
try {
if (!publicKey) return;

const daemon = await this.getByPublicKey({ pk: publicKey });
if (!daemon) return;

await this.prisma.userDevice.update({
where: {
id: daemon.id,
},
data: {
lastSeen: new Date(),
},
});
} catch (error) {
this.logger.error(error);
}
}
@OnEvent('heartbeat')
async handleHeartbeatEvent(publicKey: string) {
try {
if (!publicKey) return;

const daemon = await this.getByPublicKey({ pk: publicKey });
if (!daemon) return;

await this.prisma.userDevice.update({
where: {
id: daemon.id,
},
data: {
lastSeen: new Date(),
},
});
} catch (error) {
this.logger.error(error);
}
}
2 Replies
Prisma AI Help
Prisma AI Help3mo ago
You're in no rush, so we'll let a dev step in. Enjoy your coffee, or drop into #ask-ai if you get antsy for a second opinion!
Nurul
Nurul3mo ago
The error { code: 1020, message: "Record has changed since last read in table 'daemon'", state: "HY000" } indicates that Prisma is using optimistic concurrency control (OCC) for your update operation. This means that Prisma is checking if the record has changed since it was last read, and if it has, it throws this error to prevent overwriting changes made by another process or user. You can use updateMany instead of update. updateMany does not perform OCC checks and will update the record(s) matching the filter, regardless of whether the record has changed since it was last read.

Did you find this page helpful?