sqlite 无法将模型连接到棱柱上的另一个模型

zxlwwiss  于 2022-11-15  发布在  SQLite
关注(0)|答案(1)|浏览(154)

我正在开发一个简单的订餐项目,并使用Node.js、Express和Prisma作为处理SQlite的ORM。
这是我的实际模式。棱镜:

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "sqlite"
  url      = env("DATABASE_URL")
}

model Users {
  id        String                     @id @default(uuid())
  email     String
  name      String
  password  String
  admin     Boolean                    @default(false)
  image     String?
  orders    Orders[]
  favorites UsersHasFavoriteProducts[]
  createdAt DateTime                   @default(now()) @map("created_at")
  updatedAt DateTime                   @updatedAt @map("updated_at")

  @@map("users")
}

model UsersHasFavoriteProducts {
  id        String    @id @default(uuid())
  user      Users?    @relation(fields: [userId], references: [id])
  userId    String?   @map("user_id")
  product   Products? @relation(fields: [productId], references: [id])
  productId String?   @map("product_id")
  createdAt DateTime  @default(now()) @map("created_at")
  updatedAt DateTime  @updatedAt @map("updated_at")

  @@map("users_has_favorite_products")
}

model Orders {
  id         String              @id @default(uuid())
  totalPrice Decimal?            @map("total_price")
  payMethod  String              @default("pix") @map("pay_method")
  status     String              @default("payment pending")
  products   OrdersHasProducts[]
  user       Users?              @relation(fields: [userId], references: [id])
  userId     String?             @map("user_id")
  createdAt  DateTime            @default(now()) @map("created_at")
  updatedAt  DateTime            @updatedAt @map("updated_at")

  @@map("orders")
}

model OrdersHasProducts {
  id        String    @id @default(uuid())
  order     Orders?   @relation(fields: [orderId], references: [id])
  orderId   String?   @map("order_id")
  product   Products? @relation(fields: [productId], references: [id])
  productId String?   @map("product_id")
  quantity  Int
  createdAt DateTime  @default(now()) @map("created_at")
  updatedAt DateTime  @updatedAt @map("updated_at")

  @@map("orders_has_products")
}

model Products {
  id          String                     @id @default(uuid())
  name        String
  description String
  price       Decimal
  category    String
  ingredients ProductsHasIngredients[]
  image       String?
  orders      OrdersHasProducts[]
  favorites   UsersHasFavoriteProducts[]
  createdAt   DateTime                   @default(now()) @map("created_at")
  updatedAt   DateTime                   @updatedAt @map("updated_at")

  @@map("products")
}

model ProductsHasIngredients {
  id           String       @id @default(uuid())
  product      Products?    @relation(fields: [productId], references: [id])
  productId    String?      @map("product_id")
  ingredient   Ingredients? @relation(fields: [ingredientId], references: [id])
  ingredientId String?      @map("ingredient_id")
  createdAt    DateTime     @default(now()) @map("created_at")
  updatedAt    DateTime     @updatedAt @map("updated_at")

  @@map("products_has_ingredients")
}

model Ingredients {
  id        String                   @id @default(uuid())
  name      String
  price     Decimal
  quantity  Int
  image     String?
  products  ProductsHasIngredients[]
  createdAt DateTime                 @default(now()) @map("created_at")
  updatedAt DateTime                 @updatedAt @map("updated_at")

  @@map("ingredients")
}

控制器将收到此JSON:

{
    "name": "Tomato sandwich",
    "description": "Tomato sandwich",
    "price": "22.00",
    "category": "Main",
    "ingredients": "09ea6c56-a289-474a-ac2a-33ccd7512c6e"
}

在我的存储库上,我有一个在我的数据库中添加产品的函数:

async create(name, description, price, category, ingredients) {
    return await prisma.products.create({
      data: {
        name,
        description,
        price,
        category,
        ingredients: {
          connect: { id: ingredients },
        },
      },
    });
  }

但我无法创建与配料表的关系,出现以下错误:

The required connected records were not found. Expected 1 records to be connected after connect operation on one-to-many relation 'ProductsToProductsHasIngredients', found 
0.
juzqafwq

juzqafwq1#

基本上,配料表中没有您想要在创建产品时连接的配料。您可以执行类似创建或连接之类的操作,也可以只创建而不连接。

ingredients: {
      connectOrCreate: {
        where: {
          id: ingredients,
        },
        create: {
          id: ingredients
          ...otherRequiredFields,
        },
      },
    },

相关问题