【问题标题】:How to connect a many to many relationship using Prisma如何使用 Prisma 连接多对多关系
【发布时间】:2022-01-27 11:47:54
【问题描述】:

我正在尝试在 Prisma 多对多关系中创建和连接记录,但连接部分不适用于我。

这是我的 Prisma 模型:

model Ingredient {
  id                Int                 @id @default(autoincrement())
  name              String
  createdAt         DateTime            @default(now())
  calories          Int
  protein           Int
  fat               Int
  carbs             Int
  netCarbs          Int
  metricQuantity    Int
  metricUnit        String
  imperialQuantity  Int
  imperialUnit      String
  recipes           IngredientsOnRecipes[]
  categories        CategoriesOnIngredients[]
  }

  model IngredientCategory {
  id            Int                 @id @default(autoincrement())
  name          String
  ingredients   CategoriesOnIngredients[]
}

model CategoriesOnIngredients {
  ingredient                Ingredient     @relation(fields: [ingredientId], references: [id])
  ingredientId              Int // relation scalar field (used in the `@relation` attribute above)
  ingredientCategory        IngredientCategory @relation(fields: [ingredientCategoryId], references: [id])
  ingredientCategoryId      Int // relation scalar field (used in the `@relation` attribute above)
  assignedAt                DateTime @default(now())

  @@id([ingredientId, ingredientCategoryId])
}

这是我正在运行的 primsa 查询:

   const ingredient = await prisma.ingredient.create({
      data: {
        name: title,
        metricQuantity: parseInt(quantityMetric),
        metricUnit: unitMetric,
        imperialQuantity: parseInt(quantityImperial),
        imperialUnit: unitImperial,
        calories: parseInt(calories),
        netCarbs: parseInt(netCarbs),
        carbs: parseInt(carbs),
        protein: parseInt(protein),
        fat: parseInt(fat),
        categories: {
          ingredientcategory: {
            connect: { id: parseInt(categoryId) },
          },
        },
      },
    });

创建一种新成分非常有效,但是当我添加此部分时:

  categories: {
          ingredientcategory: {
            connect: { id: parseInt(categoryId) },
          },
        },

我收到以下错误:

未知参数 ingredientcategory 在 data.categories.ingredientcategory 中,用于 CategoriesOnIngredientsCreateNestedManyWithoutIngredientInput 类型。你的意思是createMany?可用参数: 类型 CategoriesOnIngredientsCreateNestedManyWithoutIngredientInput { create?: CategoriesOnIngredientsCreateWithoutIngredientInput |列表 | CategoriesOnIngredientsUncheckedCreateWithoutIngredientInput |列表 connectOrCreate?: CategoriesOnIngredientsCreateOrConnectWithoutIngredientInput |列表 createMany?: CategoriesOnIngredientsCreateManyIngredientInputEnvelope connect?: CategoriesOnIngredientsWhereUniqueInput |列表 }

【问题讨论】:

    标签: prisma


    【解决方案1】:

    您可以尝试执行以下操作:

    const { PrismaClient } = require('@prisma/client')
    const prisma = new PrismaClient()
    
    const saveData = async () => {
      const ingredient = await prisma.ingredient.create({
        data: {
          name: 'ingredient1',
          categories: {
            create: {
              ingredientCategory: {
                create: {
                  name: 'category1',
                },
              }
            }
          },
        },
        select: {
          id: true,
          name: true,
          categories: {
            select: {
              ingredientId: true,
              ingredientCategory: true,
            }
          },
        },
      });
    
      console.log(JSON.stringify(ingredient, null, 2));
    }
    
    saveData()
    

    您将拥有以下内容:

    【讨论】:

      【解决方案2】:

      我设法让它工作,我错过了我的 Prisma 查询中的 create:{}。

      const ingredient = await prisma.ingredient.create({
            data: {
              name: title,
              metricQuantity: parseInt(quantityMetric),
              metricUnit: unitMetric,
              imperialQuantity: parseInt(quantityImperial),
              imperialUnit: unitImperial,
              calories: parseInt(calories),
              netCarbs: parseInt(netCarbs),
              carbs: parseInt(carbs),
              protein: parseInt(protein),
              fat: parseInt(fat),
              categories: {
                create: {
                  ingredientCategory: {
                    connect: { id: parseInt(categoryId) },
                  },
                },
              },
            },
          });
      

      【讨论】:

        猜你喜欢
        • 2021-05-03
        • 1970-01-01
        • 1970-01-01
        • 2021-10-03
        • 2021-07-23
        • 2022-01-27
        • 2021-07-25
        • 2021-04-04
        • 1970-01-01
        相关资源
        最近更新 更多