【发布时间】:2023-01-14 13:33:00
【问题描述】:
我正在尝试为 MongoDB 创建一个 Prisma 模式。我的收藏对象之一如下所示:
User {
name: string;
email: string;
cart: {
items: {
productId: Types.ObjectId;
quantity: number
}[];
};
}
我遇到的问题是如何在此嵌套对象中定义用户和产品之间的关系。
到目前为止,这是我的架构。
model User {
id String @id @default(auto()) @map("_id") @db.ObjectId
v Int @map("__v")
name String
email String @unique
products Product[]
cart Cart
@@map("users")
}
type Cart {
items CartItem[]
}
type CartItem {
productId String @db.ObjectId
quantity Int
}
model Product {
id String @id @default(auto()) @map("_id") @db.ObjectId
v Int @map("__v")
title String
price Float
description String
imageUrl String
user User @relation(fields: [userId], references: [id])
userId String @db.ObjectId
@@map("products")
}
【问题讨论】:
标签: database mongodb relationship prisma