[英]How to filter by relationships using each Prisma orm
我有一個一對多的關系,我需要過濾關系值,我需要帶上所有包含數組所有值的記錄/
示例: const attributes = ["Car", "Bike", "Truck"]
prisma.car.findMany({
where: {
attribute: {
typeCar: {
every: {
value: { hasSome: attributes },
},
},
},
},
});
此搜索僅在所有attributes
都存在時才有效,如果任何屬性不存在,則不返回任何內容
我需要它來返回記錄,即使 typeCar 不包含所有記錄。
我怎么能用棱鏡做到這一點?
我的 model
model Car {
id String @id
name String
status String
description String
thumbnailUrl String?
groupId String?
categoryName String
attribute Attribute[]
images CarImage[]
createdAt DateTime @default(now())
updatedAt DateTime? @updatedAt
@@map("car")
}
model Attribute {
id String @id
typeCar String[]
car Car? @relation(fields: [carId], references: [id])
carId String?
createdAt DateTime @default(now())
updatedAt DateTime? @updatedAt
@@map("attributeProductValue")
}
我找到的一個解決方案是在car
表中添加一個新字段。 保存typeCar
的值
model Car {
id String @id
name String
status String
description String
thumbnailUrl String?
groupId String?
categoryName String
attribute Attribute[]
images CarImage[]
typeCarValues String[]
createdAt DateTime @default(now())
updatedAt DateTime? @updatedAt
@@map("car")
}
所以我可以在不通過關系的情況下進行過濾,並且hasEvery
選項有效。
示例: const attributes = ["Car", "Bike", "Truck"]
prisma.car.findMany({
where: {
typeCarValues: {
hasEvery: attributes,
},
},
})
問題在於使用every
然后hasEvery
過濾屬性中的關系,返回值typeCar
的所有attributes
都需要被告知並存在,如果沒有被告知,它不會返回。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.