Search code examples
nuxt.jsprisma

Prisma Relation query with nuxtjs


Hi I'm new to prisma and I want to connect my job and user table on apllication table.

Thx for your help

my schema is :

model Job {
  id          Int           @id @default(autoincrement())
  society     String?
  description String?
  application Application[]
}

model User {
  id           Int           @id @default(autoincrement())
  email        String        @unique
  name         String?
  applications Application[]
}

model Application {
  id     Int  @id @default(autoincrement())
  job    Job  @relation(fields: [jobId], references: [id])
  jobId  Int
  user   User @relation(fields: [userId], references: [id])
  userId Int
}

my code is :

 const application = await prisma.application.create({
                data: {
                users: {
                    connect: [{ user: {user}, userId: 1 }],
                },
                jobs: {
                    connect: [{ jobId: 1 }],
                },
                },
            })

I dont know why its not working. Thanks for help


Solution

  • In your application model, there are no users and jobs, you should use user and job instead, when you want to use connect you have to use unique value, id is enough, so use this command:

    const application = await prismaClient.application.create(
       {
        data: {
            user: {
                connect: {
                    id: 1
                }
            },
            job: {
                connect: {
                    id: 1
                }
            }
        }
       }
    )