Skip to content

Commit e3ff2b3

Browse files
committed
fix(prisma-adapter): use deleteMany when deleting by non-unique field (#8314)
1 parent 55dd06e commit e3ff2b3

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

e2e/adapter/test/adapter-factory/basic.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import type { BetterAuthPlugin } from "@better-auth/core";
2-
import type { Account, Session, User } from "@better-auth/core/db";
2+
import type {
3+
Account,
4+
Session,
5+
User,
6+
Verification,
7+
} from "@better-auth/core/db";
38
import { createTestSuite } from "@better-auth/test-utils/adapter";
49
import type {
510
Invitation,
@@ -2157,6 +2162,21 @@ export const getNormalTestSuiteTests = (
21572162
}),
21582163
).resolves.not.toThrow();
21592164
},
2165+
/**
2166+
* @see https://github.com/better-auth/better-auth/issues/8313
2167+
*/
2168+
"delete - should delete by non-unique field": async () => {
2169+
const [verification] = await insertRandom("verification");
2170+
await adapter.delete<Verification>({
2171+
model: "verification",
2172+
where: [{ field: "identifier", value: verification.identifier }],
2173+
});
2174+
const result = await adapter.findOne<Verification>({
2175+
model: "verification",
2176+
where: [{ field: "identifier", value: verification.identifier }],
2177+
});
2178+
expect(result).toBeNull();
2179+
},
21602180
"deleteMany - should delete many models": async () => {
21612181
const users = (await insertRandom("user", 3)).map((x) => x[0]);
21622182
await adapter.deleteMany({

packages/prisma-adapter/src/prisma-adapter.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,20 @@ export const prismaAdapter = (prisma: PrismaClient, config: PrismaConfig) => {
497497
`Model ${model} does not exist in the database. If you haven't generated the Prisma client, you need to run 'npx prisma generate'`,
498498
);
499499
}
500+
// Prisma's delete() requires a WhereUniqueInput (unique/primary key field).
501+
// When deleting by non-unique fields (e.g. identifier), fall back to deleteMany.
502+
const hasIdField = where?.some((w) => w.field === "id");
503+
if (!hasIdField) {
504+
const whereClause = convertWhereClause({
505+
model,
506+
where,
507+
action: "deleteMany",
508+
});
509+
await db[model]!.deleteMany({
510+
where: whereClause,
511+
});
512+
return;
513+
}
500514
const whereClause = convertWhereClause({
501515
model,
502516
where,

0 commit comments

Comments
 (0)