mysql 如何在Prisma(0 - N)中创建零到多关系

hrirmatl  于 2023-10-15  发布在  Mysql
关注(0)|答案(1)|浏览(111)

根据Prisma.io.docs的文档,没有任何关于零到多关系的说法。
我正在开发一个每日营养系统。我有以下建模,我希望InfoNutriDay至少有01食物或至少01餐。范例:

  1. InfoNutriDay可以用几种食物和不吃饭来保存。
  2. InfoNutriDay可以节省几顿饭,没有食物
  3. InfoNutriDay可以用几种食物和几顿饭来保存

我怎样才能使prisma.create正确地允许我根据上面的记录?

看看我现在的模型:

model infonutriday {
  id                     String                   @id @db.VarChar(145)
  date                   DateTime                 @db.Date
  portion                Float                    @db.Float
  protein                Float                    @db.Float
  calories               Float                    @db.Float
  grease                 Float                    @db.Float
  salt                   Float                    @db.Float
  finalizedDay           Int                      @db.TinyInt

  infonutriday_has_meals infonutriday_has_meals[]
  infonutriday_has_foods infonutriday_has_foods[]
  infonutriday_has_users infonutriday_has_users[]
}

model infonutriday_has_meals {
  infonutriday_id String       @db.VarChar(45)
  infonutriday    infonutriday @relation(fields: [infonutriday_id], references: [id])

  meals_id Int
  meals    Meal @relation(fields: [meals_id], references: [id])

  @@id([infonutriday_id, meals_id])
}

model infonutriday_has_foods {
  infonutriday_id String
  infonutriday    infonutriday @relation(fields: [infonutriday_id], references: [id])

  foods_id Int
  foods    Food @relation(fields: [foods_id], references: [id])

  @@id([infonutriday_id, foods_id])
}

model infonutriday_has_users {
  infonutriday_id String       @db.VarChar(45)
  infonutriday    infonutriday @relation(fields: [infonutriday_id], references: [id])

  users_id Int
  users    User @relation(fields: [users_id], references: [id])

  @@id([infonutriday_id, users_id])
}

在创建prisma时,请使用以下命令:

infonutriday_has_foods: foods_id ? {
                    create: {
                        foods: {
                            connect: foods_id.map((foodId: number) => (
                                {
                                    id: foodId,
                                }
                            ))
                        },
                    },
                } : undefined,

                infonutriday_has_meals: meals_id ? {
                    create: {
                        meals: {
                            connect: meals_id.map((mealId: number) => (
                                {
                                    id: mealId,
                                }
                            ))

                        },
                    },
                } : undefined,

它返回一个错误:

const newInfoNutriDay = await prisma.infonutriday.create({
        data: {
          id: "04-ago-2023+1696526834318",
          date: "2023-08-03T22:00:00.000Z",
          portion: 33,
          protein: 33,
          calories: 33,
          grease: 33,
          salt: 3,
          finalizedDay: 0,
          infonutriday_has_foods: {
            create: {
              foods: {
                connect: [
                  {
                    id: 1
                  }
                ]
                ~~~~~~~~~
              }
            }
          },
          infonutriday_has_meals: {
            create: {
              meals: {
                connect: [
                  {
                    id: 4
                  },
                  {
                    id: 3
                  }
                ]
              }
            }
          },
          infonutriday_has_users: {
            create: {
              users: {
                connect: {
                  id: 1
                }
              }
            }
          }
        },
        include: {
          infonutriday_has_foods: {
            include: {
              foods: true
            }
          },
          infonutriday_has_meals: {
            include: {
              meals: true
            }
          }
        }
      })

Argument `connect`: Invalid value provided. Expected FoodWhereUniqueInput, provided (Object).

我忘了什么?是不是还有什么和连接有关的?

a6b3iqyw

a6b3iqyw1#

我完全误解了多对多关系的概念,所有N-N都是0对多,如果它们不存在,它们就不存在于表中,仅此而已。
问题是在查询中,错误是我正在访问表infonutriday_has_foods,在这个表中,我试图创建一个新的食物并使用连接,这是不正确的,因为食物是以前创建的。
最后,我做了这个,它工作得很好:

infonutriday_has_foods: foods_id ? {
  create: foods_id.map((foodId: number) => ({
    foods: {
      connect: {
        id: foodId,
      },
    },
  })),
} : undefined,
    
infonutriday_has_meals: meals_id ? {
  create: meals_id.map((mealId: number) => ({
    meals: {
      connect: {
        id: mealId,
      },
    },
  })),
} : undefined,

相关问题