Prisma与nuxtjs的关系查询

huangapple go评论68阅读模式
英文:

Prisma Relation query with nuxtjs

问题

我是新手,想要在我的应用程序表上连接我的工作表和用户表。谢谢你的帮助。

我的模式如下:

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
}

我的代码如下:

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

我不知道为什么它不起作用。感谢帮助。

英文:

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

答案1

得分: 0

在你的应用程序模型中,没有usersjobs,你应该改为使用userjob,当你想要连接时,你必须使用唯一值,只需使用id,所以使用以下命令:

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

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
            }
        }
    }
   }
)

huangapple
  • 本文由 发表于 2023年2月24日 02:03:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75548647.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定