Skip to content

Prisma adapter foreign key problem #503

@ThePaulMcBride

Description

@ThePaulMcBride

When using the built in Prisma adapter, operations like creating a session, sets the userId foreign key directly. When using the recommend Prisma schema, this works fine. However, when you extend that schema and explicitly define the relationship between the user model and the session model, this no longer works. I've included a part of my schema below to show what it looks likes like

model Session {
  id                 String    @default(uuid()) @id
  userId             String
  user               User      @relation(fields: [userId], references: [id]) // This line seems to be where things go wrong
  accessToken        String    @default(cuid()) @unique
  sessionToken       String    @default(cuid()) @unique
  accessTokenExpires DateTime?
  expires            DateTime?
  createdAt          DateTime  @default(now())
  updatedAt          DateTime  @updatedAt @default(now())
}

Normally I'd say that I'm being an idiot and I should follow the documentation, but having that relationship defined seem to be the way Prisma recommends writing it. So much so that if you have the official Prisma plugin installed in VS Code, it'll automatically add that relationship on save.

Is it worth considering making that part of the next-auth recommended schema and following Prisma's guidelines?

Right now, my options are writing a custom Prisma adapter that uses the connect syntax for joining models, or disabling the Prisma plugin and missing out on the befits it provides.

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionAsk how to do something or how something works

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions