lucia-auth / lucia

Authentication, simple and clean
https://lucia-auth.com
MIT License
8.61k stars 448 forks source link

Cannot select both '$scalars: true' and a specific scalar field 'luciaUser'. #1427

Closed lukas-kasticky closed 5 months ago

lukas-kasticky commented 5 months ago

Package

​@lucia-auth/adapter-prisma

Describe the bug

Using a differently-named user model without adjusting the respective column names in the session model results in an error:

model LuciaUser {
  id        String         @id @default(uuid())
  oAuthId   String         @unique
  sessions  LuciaSession[]
  // ...
}

model LuciaSession {
  id        String    @id @unique
  user      LuciaUser @relation(references: [id], fields: [userId], onDelete: Cascade)
  userId    String
  expiresAt DateTime
}
PrismaClientUnknownRequestError: 
Invalid `prisma.luciaSession.findUnique()` invocation:

Cannot select both '$scalars: true' and a specific scalar field 'luciaUser'.
    at ai.handleRequestError (/tmp/node_modules/@prisma/client/runtime/library.js:126:6965)
    at ai.handleAndLogRequestError (/tmp/node_modules/@prisma/client/runtime/library.js:126:6109)
    at ai.request (/tmp/node_modules/@prisma/client/runtime/library.js:126:5817)
    at async l (/tmp/node_modules/@prisma/client/runtime/library.js:131:9709)
    at async PrismaAdapter.getSessionAndUser (file:///tmp/node_modules/@lucia-auth/adapter-prisma/dist/index.js:29:24)
    at async Lucia.validateSession (file:///tmp/node_modules/lucia/dist/core.js:61:49)
    at async Object.handle (/tmp/src/hooks.server.ts:12:29)
    at async Module.respond (/tmp/node_modules/@sveltejs/kit/src/runtime/server/respond.js:319:20)
    at async file:///tmp/node_modules/@sveltejs/kit/src/exports/vite/dev/index.js:524:22 {
  clientVersion: '5.9.1'
}

This happens because the adapter uses the model name to guess the column name: https://github.com/lucia-auth/lucia/blob/af2e48921e5378fe7fdf33fafb4d69940aaa823a/packages/adapter-prisma/src/index.ts#L41

Fix/Workaround

Simply rename the user column to userModelName. userId stays userId.

 // ...

 model LuciaSession {
   id          String    @id @unique
-  user        LuciaUser @relation(references: [id], fields: [userId], onDelete: Cascade)
+  luciaUser   LuciaUser @relation(references: [id], fields: [userId], onDelete: Cascade)
   userId      String
   expiresAt   DateTime
 }

Picking a different name for the user and session models is very useful, but I don't know how easy or sensible it is to make Lucia more flexible in that regard. I didn't find anything about this in the docs or issues, that's why I wanted to document it here at least.

lukas-kasticky commented 5 months ago

Nevermind, I'm late to the party, but maybe the error message still helps with finding this through web search

#1411