Prisma关联过滤查询报错:some参数不可用问题排查
Prisma查询问题:筛选拥有active状态CustomerDelivery的用户
问题详情
我需要查询所有关联了status为active的CustomerDelivery记录的User,但编写的查询代码运行时抛出了验证错误。
我的Prisma Schema
User模型:
model User { id Int @default(autoincrement()) @id email String @unique password String first_name String last_name String phone String? bvn String? dob String? disabled Boolean @default(false) role Roles @default(customer) verified Boolean @default(false) created_at DateTime @default(now()) updated_at DateTime @updatedAt deleted_at DateTime? verifications Verification[] SentNotifications Notification[] @relation("SenderNotification") ReceivedNotifications Notification[] @relation("ReceiverNotification") SentChats Chat[] @relation("SenderChat") ReceivedChats Chat[] @relation("ReceiverChat") Goods Good[] CustomerDeliveries CustomerDelivery[] //1-m relationship CustomerDeliveryLists CustomerDeliveryList[] Addresses Address[] UserNairaAccounts UserNairaAccount[] UserDollarAccounts UserDollarAccount[] Cards Card[] ActivityLogs activityLog[] }
CustomerDelivery模型:
model CustomerDelivery { id Int @default(autoincrement()) @id DeliveryRate DeliveryRate? @relation(fields: [delivery_rate_id], references: [id]) delivery_rate_id Int? Customer User @relation(fields: [customer_id], references: [id]) customer_id Int status DeliveryStatus @default(active) created_at DateTime @default(now()) updated_at DateTime @updatedAt deleted_at DateTime? CustomerDeliveryGoods CustomerDeliveryGoods[] CustomerDeliveryListToCustomerDelivery CustomerDeliveryListToCustomerDelivery[] }
编写的查询代码
return await this.db.user.findMany({ where: { CustomerDeliveries: { some: { status: { equals: 'active' } } } }, orderBy: { created_at: 'desc' }, include: { CustomerDeliveries: true }, skip: offset, take: limit })
抛出的错误
PrismaClientValidationError: Invalid `this.db.user.findMany()` invocation in /Users/macbook/Desktop/cart/src/users/user.repository.ts:390:35 387 }) 388 389 → 390 return await this.db.user.findMany({ where: { CustomerDeliveries: { some: { ~~~~ status: { equals: 'active' } } } }, orderBy: { created_at: 'desc' }, include: { CustomerDeliveries: true }, skip: 0, take: 50 }) Unknown arg `some` in where.CustomerDeliveries.some for type CustomerDeliveryListRelationFilter. Did you mean `is`? Available args: type CustomerDeliveryListRelationFilter { is?: CustomerDeliveryListWhereInput | Null isNot?: CustomerDeliveryListWhereInput | Null }
解决方案
1. 重新生成Prisma客户端
错误提示里出现了对应CustomerDeliveryLists字段的CustomerDeliveryListRelationFilter,但你查询的是CustomerDeliveries,说明Prisma客户端没有正确同步最新的schema定义。执行以下命令重新生成客户端:
npx prisma generate
2. 调整查询语法(如果重新生成后仍报错)
如果你的Prisma版本低于2.14.0,不支持在多关系字段的where条件中使用some语法,可以改用exists子查询实现需求:
return await this.db.user.findMany({ where: { exists: { CustomerDeliveries: { status: 'active' } } }, orderBy: { created_at: 'desc' }, include: { CustomerDeliveries: true }, skip: offset, take: limit })
如果你的Prisma版本足够新,重新生成客户端后some语法应该可以正常使用,注意status的值要和DeliveryStatus枚举的定义完全匹配(比如是否是小写的active)。
内容的提问来源于stack exchange,提问作者Franklin Ekoh
相关产品推荐
相关产品推荐

