[GH-ISSUE #2431] SSO Provider Plugin Schema is invalid #9190

Closed
opened 2026-04-13 04:34:03 -05:00 by GiteaMirror · 3 comments
Owner

Originally created by @Snazzie on GitHub (Apr 24, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/2431

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

  1. bunx @better-auth/cli@latest generate
  2. bunx prisma migrate dev

Current vs. Expected behavior

generated schema has validation issues. Expect no validation issues.

What version of Better Auth are you using?

1.2.7

Provide environment information

windows 11

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

Package

Auth config (if applicable)

import { betterAuth } from 'better-auth';
import { prismaAdapter } from 'better-auth/adapters/prisma';
import { openAPI } from 'better-auth/plugins';
import { sso } from 'better-auth/plugins/sso';
import { env } from '../utils/envHelper';

import { PrismaClient } from '@prisma/client';

const prismaClient = new PrismaClient();
export const auth = betterAuth({
  secret: env.BETTER_AUTH_SECRET,
  database: prismaAdapter(prismaClient, {
    provider: 'sqlite',
  }),
  emailAndPassword: {
    enabled: true,
    requireEmailVerification: false,
  },
  plugins: [openAPI(), sso()],
});

export type AuthType = {
  Variables: {
    user: typeof auth.$Infer.Session.user | null;
    session: typeof auth.$Infer.Session.session | null;
  };
};

Additional context

Image

Error: Prisma schema validation - (validate wasm)
Error code: P1012
error: Error validating: The relation field user uses the scalar fields userId. At least one of those fields is optional. Hence the relation field must be optional as well.
--> prisma\schema.prisma:126
|
125 | userId String?
126 | user User @relation(fields: [userId], references: [id], onDelete: Cascade)
127 | providerId String
|

Originally created by @Snazzie on GitHub (Apr 24, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/2431 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce 1. bunx @better-auth/cli@latest generate 2. bunx prisma migrate dev ### Current vs. Expected behavior generated schema has validation issues. Expect no validation issues. ### What version of Better Auth are you using? 1.2.7 ### Provide environment information ```bash windows 11 ``` ### Which area(s) are affected? (Select all that apply) Package ### Auth config (if applicable) ```typescript import { betterAuth } from 'better-auth'; import { prismaAdapter } from 'better-auth/adapters/prisma'; import { openAPI } from 'better-auth/plugins'; import { sso } from 'better-auth/plugins/sso'; import { env } from '../utils/envHelper'; import { PrismaClient } from '@prisma/client'; const prismaClient = new PrismaClient(); export const auth = betterAuth({ secret: env.BETTER_AUTH_SECRET, database: prismaAdapter(prismaClient, { provider: 'sqlite', }), emailAndPassword: { enabled: true, requireEmailVerification: false, }, plugins: [openAPI(), sso()], }); export type AuthType = { Variables: { user: typeof auth.$Infer.Session.user | null; session: typeof auth.$Infer.Session.session | null; }; }; ``` ### Additional context ![Image](https://github.com/user-attachments/assets/baa8b7f5-23bf-4bda-bf4b-5c16c0ab613a) Error: Prisma schema validation - (validate wasm) Error code: P1012 error: Error validating: The relation field `user` uses the scalar fields userId. At least one of those fields is optional. Hence the relation field must be optional as well. --> prisma\schema.prisma:126 | 125 | userId String? 126 | user User @relation(fields: [userId], references: [id], onDelete: Cascade) 127 | providerId String |
GiteaMirror added the lockedbug labels 2026-04-13 04:34:03 -05:00
Author
Owner

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

Hello @Snazzie I have tried to address the issue.
I've explained it in this PR

For reference, you can read:
When we run bunx @better-auth/cli@latest generate, prisma will follow its naming convention and will name Session[] -> sessions and not Session, the schema already had two such params Session (as Session[]) and Account (as Account[]), there by duplicate sessions and accounts, causing issue. Renaming the existing Session and Account with sessions and accounts, hence averting the issue. Furthermore, in SsoProvider model have userId which is optional, but User (which contains userId) was a mandate, that's not possible. So made User also as optional.

Do let me know, if there are anything I have missed out. I will be happy to dive in and figure out the problem

After successful migration

Image

Image

Cheers,
Suvadeep Ghoshal (iamsg97)

<!-- gh-comment-id:2906518979 --> @iamsg97 commented on GitHub (May 24, 2025): Hello @Snazzie I have tried to address the issue. I've explained it in this [PR](https://github.com/better-auth/better-auth/pull/2771) For reference, you can read: When we run `bunx @better-auth/cli@latest generate`, `prisma` will follow its naming convention and will name `Session[]` -> `sessions` and not `Session`, the schema already had two such params `Session` (as `Session[]`) and `Account` (as `Account[]`), there by duplicate `sessions` and `accounts`, causing issue. Renaming the existing `Session` and `Account` with `sessions` and `accounts`, hence averting the issue. Furthermore, in `SsoProvider` model have `userId` which is optional, but `User` (which contains `userId`) was a mandate, that's not possible. So made `User` also as optional. Do let me know, if there are anything I have missed out. I will be happy to dive in and figure out the problem After successful migration ![Image](https://github.com/user-attachments/assets/cba091d7-dfc2-4e64-890f-4d4b7ebea0ad) ![Image](https://github.com/user-attachments/assets/c1210a85-70dd-4e46-9fca-4b1f740a5d7d) Cheers, Suvadeep Ghoshal (iamsg97)
Author
Owner

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

yeah renaming fixes it. prisma has caused a load more issues since and ive decided to move to drizzle.

<!-- gh-comment-id:2907138539 --> @Snazzie commented on GitHub (May 24, 2025): yeah renaming fixes it. prisma has caused a load more issues since and ive decided to move to drizzle.
Author
Owner

@iamsg97 commented on GitHub (May 25, 2025):

Yes! I saw the maintainers removed prisma directory from bun, guess it's not a requirement anymore.
Cheers

<!-- gh-comment-id:2907933752 --> @iamsg97 commented on GitHub (May 25, 2025): Yes! I saw the maintainers removed prisma directory from bun, guess it's not a requirement anymore. Cheers
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#9190