我正在開發一個簡單的食品訂購專案,我正在使用 Node.js、Express 和 Prisma 作為 ORM 來處理 SQlite。
這是我的實際 schema.prisma:
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.
uj5u.com熱心網友回復:
基本上,成分表沒有您在創建產品時想要連接的成分。您可以執行創建或連接之類的操作,或者僅創建而不是連接。
ingredients: {
connectOrCreate: {
where: {
id: ingredients,
},
create: {
id: ingredients
...otherRequiredFields,
},
},
},
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/503965.html
