[GH-ISSUE #2114] Invalid db[getModelName(model)].delete() invocation with Prisma and Next.js #9056

Closed
opened 2026-04-13 04:20:30 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @kirill-dev-pro on GitHub (Apr 4, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/2114

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

So, im getting this error:

prisma:error
Invalid `db[getModelName(model)].delete()` invocation in
/my-project-dir/.next/server/chunks/node_modules_better-auth_dist_710e70._.js:3241:57

  3238 const { model, where } = data;
  3239 const whereClause = convertWhereClause(model, where);
  3240 try {
→ 3241     await db[getModelName(model)].delete({
             where: {
               token: "ZQ0jqvXPhgih3vEl7HwU07Kb1TFpGo0A",
           ?   id?: String,
           ?   AND?: SessionWhereInput | SessionWhereInput[],
           ?   OR?: SessionWhereInput[],
           ?   NOT?: SessionWhereInput | SessionWhereInput[],
           ?   userId?: StringFilter | String,
           ?   expiresAt?: DateTimeFilter | DateTime,
           ?   ipAddress?: StringNullableFilter | String | Null,
           ?   userAgent?: StringNullableFilter | String | Null,
           ?   createdAt?: DateTimeFilter | DateTime,
           ?   updatedAt?: DateTimeFilter | DateTime,
           ?   user?: UserScalarRelationFilter | UserWhereInput
             }
           })

Argument `where` of type SessionWhereUniqueInput needs at least one of `id` arguments. Available options are marked with ?.
 POST /api/auth/sign-out 200 in 43ms

When im signing out. Looks like prisma adapter cant delete sessions data. It deletes cookies but not db session

Current vs. Expected behavior

I would expect not seeing this error

What version of Better Auth are you using?

1.2.5

Provide environment information

- MacOS
- bun
- Next.js

Which area(s) are affected? (Select all that apply)

Backend

Auth config (if applicable)

export const authConfig = {
  appName: APP_NAME,
  secret: env.BETTER_AUTH_SECRET,
  baseURL: env.NEXT_PUBLIC_APP_URL,
  trustedOrigins: [env.NEXT_PUBLIC_APP_URL],
  logger: {
    disabled: process.env.NODE_ENV === 'production',
    level: 'debug',
  },
  database: prismaAdapter(prisma, {
    provider: 'postgresql',
  }),
  user: {
    modelName: 'user',
    changeEmail: {
      enabled: true,
    },
    deleteUser: {
      enabled: true,
    },
  },
  account: {
    accountLinking: {
      enabled: true,
      trustedProviders: ['google'],
    },
    modelName: 'account',
  },
  session: {
    freshAge: 5 * 60, // 5 minutes
    expiresIn: 60 * 60 * 24 * 3, // 3 days
    updateAge: 60 * 60 * 12, // 12 hours (every 12 hours the session expiration is updated)
    cookieCache: {
      enabled: true,
      maxAge: 60 * 30, // 30 minutes
    },
  },
  emailAndPassword: {
    enabled: true,
    requireEmailVerification: false,
  },
  socialProviders: {
    google: {
      clientId: env.GOOGLE_CLIENT_ID,
      clientSecret: env.GOOGLE_CLIENT_SECRET,
    },
  },
}

Additional context

Ref deleteSession method fcb43aa7b9/packages/better-auth/src/db/internal-adapter.ts (L442)
Ref delete method in prisma adapter
fcb43aa7b9/packages/better-auth/src/adapters/prisma-adapter/prisma-adapter.ts (L278)

Originally created by @kirill-dev-pro on GitHub (Apr 4, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/2114 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce So, im getting this error: ``` prisma:error Invalid `db[getModelName(model)].delete()` invocation in /my-project-dir/.next/server/chunks/node_modules_better-auth_dist_710e70._.js:3241:57 3238 const { model, where } = data; 3239 const whereClause = convertWhereClause(model, where); 3240 try { → 3241 await db[getModelName(model)].delete({ where: { token: "ZQ0jqvXPhgih3vEl7HwU07Kb1TFpGo0A", ? id?: String, ? AND?: SessionWhereInput | SessionWhereInput[], ? OR?: SessionWhereInput[], ? NOT?: SessionWhereInput | SessionWhereInput[], ? userId?: StringFilter | String, ? expiresAt?: DateTimeFilter | DateTime, ? ipAddress?: StringNullableFilter | String | Null, ? userAgent?: StringNullableFilter | String | Null, ? createdAt?: DateTimeFilter | DateTime, ? updatedAt?: DateTimeFilter | DateTime, ? user?: UserScalarRelationFilter | UserWhereInput } }) Argument `where` of type SessionWhereUniqueInput needs at least one of `id` arguments. Available options are marked with ?. POST /api/auth/sign-out 200 in 43ms ``` When im signing out. Looks like prisma adapter cant delete sessions data. It deletes cookies but not db session ### Current vs. Expected behavior I would expect not seeing this error ### What version of Better Auth are you using? 1.2.5 ### Provide environment information ```bash - MacOS - bun - Next.js ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript export const authConfig = { appName: APP_NAME, secret: env.BETTER_AUTH_SECRET, baseURL: env.NEXT_PUBLIC_APP_URL, trustedOrigins: [env.NEXT_PUBLIC_APP_URL], logger: { disabled: process.env.NODE_ENV === 'production', level: 'debug', }, database: prismaAdapter(prisma, { provider: 'postgresql', }), user: { modelName: 'user', changeEmail: { enabled: true, }, deleteUser: { enabled: true, }, }, account: { accountLinking: { enabled: true, trustedProviders: ['google'], }, modelName: 'account', }, session: { freshAge: 5 * 60, // 5 minutes expiresIn: 60 * 60 * 24 * 3, // 3 days updateAge: 60 * 60 * 12, // 12 hours (every 12 hours the session expiration is updated) cookieCache: { enabled: true, maxAge: 60 * 30, // 30 minutes }, }, emailAndPassword: { enabled: true, requireEmailVerification: false, }, socialProviders: { google: { clientId: env.GOOGLE_CLIENT_ID, clientSecret: env.GOOGLE_CLIENT_SECRET, }, }, } ``` ### Additional context Ref `deleteSession` method https://github.com/better-auth/better-auth/blob/fcb43aa7b96428319e67605494758cbfc4c14965/packages/better-auth/src/db/internal-adapter.ts#L442 Ref `delete` method in prisma adapter https://github.com/better-auth/better-auth/blob/fcb43aa7b96428319e67605494758cbfc4c14965/packages/better-auth/src/adapters/prisma-adapter/prisma-adapter.ts#L278
GiteaMirror added the lockedbug labels 2026-04-13 04:20:30 -05:00
Author
Owner

@kirill-dev-pro commented on GitHub (Apr 4, 2025):

Ok, it seems it was a problem in my db schema. Prisma wants to have @unique on the field that is used to delete exactly one row.
now my schema.prisma looks like this and it solved error

model Session {
  id        String   @id @default(cuid(2)) @map("_id")
  userId    String
  token     String   @unique
  expiresAt DateTime
  ipAddress String?
  userAgent String?
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  user User @relation(fields: [userId], references: [id], onDelete: Cascade)

  @@map("session")
}
<!-- gh-comment-id:2778511157 --> @kirill-dev-pro commented on GitHub (Apr 4, 2025): Ok, it seems it was a problem in my db schema. Prisma wants to have `@unique` on the field that is used to delete exactly one row. now my schema.prisma looks like this and it solved error ```schema.prisma model Session { id String @id @default(cuid(2)) @map("_id") userId String token String @unique expiresAt DateTime ipAddress String? userAgent String? createdAt DateTime @default(now()) updatedAt DateTime @updatedAt user User @relation(fields: [userId], references: [id], onDelete: Cascade) @@map("session") } ```
Author
Owner

@rikarani commented on GitHub (May 24, 2025):

@kirill-dev-pro thankss

<!-- gh-comment-id:2906994531 --> @rikarani commented on GitHub (May 24, 2025): @kirill-dev-pro thankss
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#9056