next-auth icon indicating copy to clipboard operation
next-auth copied to clipboard

Unknown field `user` for select statement on model Account. Available options are listed in green. Did you mean `User`?

Open chopshop1 opened this issue 2 years ago • 5 comments

Adapter type

@next-auth/prisma-adapter

Environment

System: OS: macOS 12.4 CPU: (10) arm64 Apple M1 Max Memory: 1.53 GB / 32.00 GB Shell: 5.8.1 - /bin/zsh Binaries: Node: 16.15.1 - /usr/local/bin/node Yarn: 1.22.17 - /usr/local/bin/yarn npm: 8.11.0 - /usr/local/bin/npm Browsers: Safari: 15.5 npmPackages: next: 12.2.2 => 12.2.2 next-auth: ^4.10.2 => 4.10.2 react: 18.2.0 => 18.2.0

npmPackages: @next-auth/prisma-adapter: ^1.0.4 => 1.0.4

Reproduction URL

https://next-auth.js.org/adapters/prisma

Describe the issue

when logging with a new users this error pops up

Unknown field `user` for select statement on model Account. Available options are listed in green. Did you mean `User`?

Error Message:

   8 getUserByEmail: (email) => p.user.findUnique({ where: { email } }),
   9 async getUserByAccount(provider_providerAccountId) {
  10     var _a;
→ 11     const account = await p.account.findUnique({
           where: {
             provider_providerAccountId: {
               providerAccountId: '101458821972188023311',
               provider: 'google'
             }
           },
           select: {
             user: true,
             ~~~~
         ?   id?: true,
         ?   userId?: true,
         ?   type?: true,
         ?   provider?: true,
         ?   providerAccountId?: true,
         ?   refresh_token?: true,
         ?   access_token?: true,
         ?   expires_at?: true,
         ?   token_type?: true,
         ?   scope?: true,
         ?   id_token?: true,
         ?   session_state?: true,
         ?   User?: true
           }
         })


Unknown field `user` for select statement on model Account. Available options are listed in green. Did you mean `User`?
 {
  message: '\n' +
    'Invalid `p.account.findUnique()` invocation in\n' +
    '.../frontend/node_modules/@next-auth/prisma-adapter/dist/index.js:11:45\n' +
    '\n' +
    '   8 getUserByEmail: (email) => p.user.findUnique({ where: { email } }),\n' +
    '   9 async getUserByAccount(provider_providerAccountId) {\n' +
    '  10     var _a;\n' +
    '→ 11     const account = await p.account.findUnique({\n' +
    '           where: {\n' +
    '             provider_providerAccountId: {\n' +
    "               providerAccountId: '101458821972188023311',\n" +
    "               provider: 'google'\n" +
    '             }\n' +
    '           },\n' +
    '           select: {\n' +
    '             user: true,\n' +
    '             ~~~~\n' +
    '         ?   id?: true,\n' +
    '         ?   userId?: true,\n' +
    '         ?   type?: true,\n' +
    '         ?   provider?: true,\n' +
    '         ?   providerAccountId?: true,\n' +
    '         ?   refresh_token?: true,\n' +
    '         ?   access_token?: true,\n' +
    '         ?   expires_at?: true,\n' +
    '         ?   token_type?: true,\n' +
    '         ?   scope?: true,\n' +
    '         ?   id_token?: true,\n' +
    '         ?   session_state?: true,\n' +
    '         ?   User?: true\n' +
    '           }\n' +
    '         })\n' +
    '\n' +
    '\n' +
    'Unknown field `user` for select statement on model Account. Available options are listed in green. Did you mean `User`?\n',
  stack: 'Error: \n' +
    'Invalid `p.account.findUnique()` invocation in\n' +
    '.../frontend/node_modules/@next-auth/prisma-adapter/dist/index.js:11:45\n' +
    '\n' +
    '   8 getUserByEmail: (email) => p.user.findUnique({ where: { email } }),\n' +
    '   9 async getUserByAccount(provider_providerAccountId) {\n' +
    '  10     var _a;\n' +
    '→ 11     const account = await p.account.findUnique({\n' +
    '           where: {\n' +
    '             provider_providerAccountId: {\n' +
    "               providerAccountId: '101458821972188023311',\n" +
    "               provider: 'google'\n" +
    '             }\n' +
    '           },\n' +
    '           select: {\n' +
    '             user: true,\n' +
    '             ~~~~\n' +
    '         ?   id?: true,\n' +
    '         ?   userId?: true,\n' +
    '         ?   type?: true,\n' +
    '         ?   provider?: true,\n' +
    '         ?   providerAccountId?: true,\n' +
    '         ?   refresh_token?: true,\n' +
    '         ?   access_token?: true,\n' +
    '         ?   expires_at?: true,\n' +
    '         ?   token_type?: true,\n' +
    '         ?   scope?: true,\n' +
    '         ?   id_token?: true,\n' +
    '         ?   session_state?: true,\n' +
    '         ?   User?: true\n' +
    '           }\n' +
    '         })\n' +
    '\n' +
    '\n' +
    'Unknown field `user` for select statement on model Account. Available options are listed in green. Did you mean `User`?\n' +
    '\n' +
    '    at Document.validate (.../frontend/node_modules/@prisma/client/runtime/index.js:27181:20)\n' +
    '    at PrismaClient._executeRequest (.../frontend/node_modules/@prisma/client/runtime/index.js:29694:17)\n' +
    '    at async consumer (.../frontend/node_modules/@prisma/client/runtime/index.js:29618:18)\n' +
    '    at async PrismaClient._request (.../frontend/node_modules/@prisma/client/runtime/index.js:29639:16)\n' +
    '    at async getUserByAccount (.../frontend/node_modules/@next-auth/prisma-adapter/dist/index.js:11:29)',
  name: 'Error'
}

How to reproduce

follow guide in https://next-auth.js.org/adapters/prisma

Expected behavior

new user gets created on first login

chopshop1 avatar Jul 25 '22 21:07 chopshop1

please add a reproduction

balazsorban44 avatar Jul 25 '22 23:07 balazsorban44

@balazsorban44 just follow guide in https://next-auth.js.org/adapters/prisma with a fresh next + prisma project, and you will be able to reproduce the same issue. Or where you looking for something else?

chopshop1 avatar Jul 26 '22 16:07 chopshop1

@balazsorban44 ^ 🙏

chopshop1 avatar Aug 06 '22 19:08 chopshop1

same problem here, any updates?

Sumis34 avatar Aug 27 '22 17:08 Sumis34

Make sure in your schema.prisma the user in the model description is not Capitalized.

fulopdaniel avatar Sep 13 '22 10:09 fulopdaniel

this was my Schema to solve the error

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider     = "mysql"
  url          = env("DATABASE_URL")
  relationMode = "prisma"
}

model Account {
  user              User    @relation(fields: [userId], references: [id], onDelete: Cascade)
  id                String  @id @default(cuid())
  userId            String
  type              String
  provider          String
  providerAccountId String
  refresh_token     String? @db.Text
  access_token      String? @db.Text
  expires_at        Int?
  token_type        String?
  scope             String?
  id_token          String? @db.Text
  session_state     String?

  @@unique([provider, providerAccountId])
}

model Session {
  id           String   @id @default(cuid())
  sessionToken String   @unique
  userId       String
  expires      DateTime
  user         User     @relation(fields: [userId], references: [id], onDelete: Cascade)
}

model User {
  id              String    @id @default(cuid())
  user            String
  name            String?
  email           String?   @unique
  emailVerified   DateTime?
  image           String?
  accounts        Account[]
  sessions        Session[]
  accessGranted   Boolean   @default(false)
  isAdministrator Boolean   @default(false)
}

model VerificationToken {
  identifier String
  token      String   @unique
  expires    DateTime

  @@unique([identifier, token])
}

I also returned the user data manually

  providers: [
    GoogleProvider({
      clientId: env.GOOGLE_CLIENT_ID,
      clientSecret: env.GOOGLE_CLIENT_SECRET,
      profile(profile) {
        return {
          id: profile.sub,
          name: profile.name,
          user: profile.given_name,
          email: profile.email,
          image: profile.picture,
        };
      },
    }),
    // ...add more providers here
  ],

NathanLazo avatar Jan 26 '23 03:01 NathanLazo

@NathanLazo can you point out what change fixed it?

JulianKingman avatar Jul 17 '23 15:07 JulianKingman

Added user string field on User prisma model and also returned user data manually from provider

NathanLazo avatar Aug 14 '23 18:08 NathanLazo