Prisma嵌套查询报错:PropertyLocationCompositeFilter未知location参数
问题:根据国家筛选房源查询报错
需求
根据国家名称及其他参数查询Listing数据,返回对应国家的房源。
数据Schema
type Location { city String state String country String } type PropertyLocation { address String location Location latlng Float[] } model Listing { id String @id @default(auto()) @map("_id") @db.ObjectId title String description String imageSrc String detailImage String[] createdAt DateTime @default(now()) dateModified DateTime @default(now()) category String roomCount Int bathroomCount Int guestCount Int location PropertyLocation userId String @db.ObjectId price Int review Float isActive Boolean @default(true) reservations Reservation[] user User @relation(fields: [userId], references: [id], onDelete: Cascade) }
当前查询代码
if (locationValue) query.location = { location: { country: locationValue, }, }; const listings = await dbClient.listing.findMany({ where: query, orderBy: { createdAt: "desc", }, });
报错信息
Unknown arg `location` in where.location.location for type PropertyLocationCompositeFilter. Available args: type PropertyLocationCompositeFilter { equals?: PropertyLocationObjectEqualityInput is?: PropertyLocationWhereInput isNot?: PropertyLocationWhereInput }
解决方案
针对Prisma的复合类型过滤规则,需要使用is关键字来指定嵌套对象的匹配条件,修改后的代码如下:
if (locationValue) { query.location = { is: { location: { country: locationValue, }, }, }; } const listings = await dbClient.listing.findMany({ where: query, orderBy: { createdAt: "desc", }, });
原因:PropertyLocation属于复合过滤类型,Prisma要求通过is/isNot来定义其内部字段的匹配逻辑,直接嵌套字段名会被识别为非法参数。
内容的提问来源于stack exchange,提问作者Slowcode_beef
相关产品推荐
相关产品推荐

