简体   繁体   English

我在 prisma 一对多关系中有两个 model 但我想将数据插入第二个 model 怎么办?

[英]I have two model in prisma one to many relation but i want to insert data into second model how to do this?

I have two model in prisma - one package model and second variation model. They are in one to many relation我在 prisma 中有两个 model - 一个 package model 和第二个变体 model。它们是一对多关系

model Package {
  id Int @id @default(autoincrement())
  name String
  title String
  vocavive_user vocavive_user[]
  variations Variation[]
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

model Variation {
  id Int @id @default(autoincrement())
  expiration Int
  bdt Int 
  discount_bdt Int @default(00)
  usd Int @default(00)
  discount_usd Int @default(00)
  status Boolean @default(false)
  packages Package? @relation(fields: [package_id], references: [id], onDelete: Cascade)
  package_id Int?
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

I can insert data variation when I insert package data and variation data at the same time.当我同时插入 package 数据和变化数据时,我可以插入数据变化。

exports.createPackage = catchAsync(async (req, res) => {
  const {
    name,
    title,
    expiration,
    bdt,
    discount_bdt,
    usd,
    discount_usd,
    status,
  } = req.body;
  const newPackage = await prisma.package.create({
    data: {
      name: name,
      title: title,
      variations: {
        create: {
          expiration,
          bdt,
          discount_bdt,
          usd,
          discount_usd,
          status,
        },
      },
    },
    include: {
      variations: true,
    },
  });
  console.log(newPackage);
  res.status(200).json(newPackage);
});

But in my case I need to insert value only into variation table.. I tried this但在我的例子中,我只需要将值插入到变化表中。我试过了

exports.createVariation = catchAsync(async (req, res) => {
  const newVariation = await prisma.variation.create({
    data: {
      expiration: req.body.expiration,
      bdt: req.body.bdt,
      package_id: req.body.package_id,
    },
  });
  res.status(200).json(`variation Created Successfully: ${newVariation}`);
});

but it shows me但它告诉我

38 exports.createVariation = catchAsync(async (req, res) => { 39 const newVariation = await prisma.variation.create( Foreign key constraint failed on the field: Variation_package_id_fkey (index) at RequestHandler.handleRequestError (D:\Firoz\demo_authentication\node_modules@prisma\client\runtime\index.js:34869:13) at RequestHandler.handleAndLogRequestError (D:\Firoz\demo_authentication\node_modules@prisma\client\runtime\index.js:34841:12) at RequestHandler.request (D:\Firoz\demo_authentication\node_modules@prisma\client\runtime\index.js:34836:12) at async PrismaClient._request (D:\Firoz\demo_authentication\node_modules@prisma\client\runtime\index.js:35926:16) at async D:\Firoz\demo_authentication\controllers\packageController.js:39:24 Error: Invalid prisma.variation.create() invocation in 38 exports.createVariation = catchAsync(async (req, res) => { 39 const newVariation = await prisma.variation.create( 外键约束在字段上失败: Variation_package_id_fkey (index) at RequestHandler.handleRequestError (D:\Firoz\demo_authentication \node_modules@prisma\client\runtime\index.js:34869:13) 在 RequestHandler.handleAndLogRequestError (D:\Firoz\demo_authentication\node_modules@prisma\client\runtime\index.js:34841:12) 在 RequestHandler.request ( D:\Firoz\demo_authentication\node_modules@prisma\client\runtime\index.js:34836:12) 在异步 PrismaClient._request (D:\Firoz\demo_authentication\node_modules@prisma\client\runtime\index.js:35926: 16) 在异步 D:\Firoz\demo_authentication\controllers\packageController.js:39:24 错误:无效prisma.variation.create()调用

How can I solve this problem?我怎么解决这个问题?

The error message suggests that there doesn't exist a package with id that you are providing in package_id field.该错误消息表明不存在您在package_id字段中提供的 id 为 package 的情况。

You can omit the package_id field if it isn't needed or you would need to make sure that package with the value of package_id you are passing exists.如果不需要,您可以省略package_id字段,或者您需要确保 package 和您传递的 package_id 的值存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将 prisma 与 postgreSql 一起使用,但我在模型中有一个以数字开头的字段 - Using prisma with postgreSql but I have a field in model starting with number 我如何在MySQL Nodejs中建立一对多关系? - How can I have one-to-many relation in MySQL Nodejs? 如何从“多个”模型中包含关系的“很多”部分? - How to include the 'many' part of a relation from the 'one of many' model? 如何在 TypeORM Model 中保持一对多关系的“多”方唯一? - How do I keep the "many" side of a one-to-many relationship unique in a TypeORM Model? Sequelize-如果与另一个模型存在关系,如何“选择”模型的实例 - Sequelize - How do I “select” instances of a model if a relation exists to another model 如何使用存储库和一对多关系插入数据? - How to insert data with use repository and relation one-to-many? 我怎样才能使查询 model 关系有很多易于阅读? - How can I make query model relation has to many easy to read? 我想在Loopback中为此JSON数据创建模型 - I want to create a model for this JSON data in Loopback 我在 prisma 中使用 nest js 和 mongodb,我想添加三个集合,我该怎么做? - i'm using nest js and mongodb with prisma and i want to add three collection in how can i do? 将数据从一个模型分为两个模型后,如何重写猫鼬查询? - how can I rewrite my mongoose query after splitting data from one model into two?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM