【问题标题】:MongoDB + Prima relationships in nested objects嵌套对象中的 MongoDB + Prima 关系
【发布时间】: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


    【解决方案1】:

    你有没有想过这个?我遇到了同样的问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-19
      • 2016-06-25
      • 1970-01-01
      • 1970-01-01
      • 2016-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多