Prisma 2 -Unkown arg'其中'在select.fruit。您的意思是select&#x27 ;?可用的Args
试图查询Prisma并过滤来自相关对象的结果,但要获得错误:
select.fruit.s in type type用户弗鲁特(select.fruit.)中的未知arg'where'''''''在哪里。你了吗? 是指“选择”?可用的args果实{}
async findShops(req) {
const userId = parseInt(req.params.id);
const shop = await prisma.shop.findMany({
select: {
id: true,
name: true,
logo: true,
fruit:{
select:{
id:true,
userId:true,
fruitNumber:true,
created: true,
updated: true,
},
where: {
userId: userId
}
}
}
})
return shop;
};
示例有效载荷预期:
[
{ id: 1001, name: 'test1', logo: 'log.png', fruit: null },
{ id: 1002, name: 'test2', logo: 'log2.jpg', fruit: null },
{ id: 1003, name: 'test3', logo: 'log3.jpg', fruit: null },
{
id: 1005,
name: 'test4',
logo: 'log4.png',
fruit: {
id: '62450ee5-e75d-4a67-8d79-120d11ddf508',
userId: 111,
fruitNumber: '123456',
created: 2022-07-01T06:39:52.924Z,
updated: 2022-07-01T06:39:52.936Z
}
},
{
id: 1004,
name: 'test5',
logo: 'log5.jpg',
fruit: {
id: '20e9af37-2e6f-4070-8475-c5a914f311dc',
userId: 111,
fruitNumber: '123878',
created: 2022-07-01T07:21:27.898Z,
updated: 2022-07-01T07:21:27.901Z
}
}
]
可以通过没有“哪里”来轻松实现预期的输出
fruit: {
id: '62450ee5-e75d-4a67-8d79-120d11ddf508',
userId: 111,
fruitNumber: '123456',
created: 2022-07-01T06:39:52.924Z,
updated: 2022-07-01T06:39:52.936Z
},
{
id: '62450ee5-e75d-4a67-8d79-120d11ddf508',
userId: 999,
fruitNumber: '123456',
created: 2022-07-01T06:39:52.924Z,
updated: 2022-07-01T06:39:52.936Z
}
我 需要null和与用户ID匹配的任何东西,并且基于设计,对于特定用户来说,每个商店只能是1个记录。
在某种程度上,我的代码似乎起作用,但是在我做一个Prisma产生后,它停止了工作。有其他方法可以达到相同的结果,还是有其他方法可以解决此问题?
注意:下面的版本信息。
model UserFruit {
id String @id @default(uuid())
fruitNumber String @map("fruit_number")
shopId Int @unique @map("shop_id")
userId Int @map("user_id")
created DateTime @default(now())
updated DateTime @updatedAt
fruit Fruit @relation(fields: [fruitId], references: [id])
@@unique([userId, fruitId], name: "userFruit")
@@map("user_Fruit")
}
model Shop {
id Int @id @default(autoincrement())
name String @unique
logo String
created DateTime @default(now())
updated DateTime @updatedAt
fruit UserFruit?
@@map("Shop")
}
model User {
id Int @id @default(autoincrement())
created DateTime @default(now())
updated DateTime @updatedAt
uid String @unique
email String @unique
phone String @unique
firstName String @map("first_name")
lastName String @map("last_name")
dob DateTime?
gender String?
roleId Int @default(1) @map("role_id")
role Role @relation(fields: [roleId], references: [id])
@@map("user")
}
Trying to query in prisma and filter results from a related object but get the error:
Unknown arg 'where' in select.fruit.where for type UserFruit. Did you
mean 'select'? Available args fruit{}
async findShops(req) {
const userId = parseInt(req.params.id);
const shop = await prisma.shop.findMany({
select: {
id: true,
name: true,
logo: true,
fruit:{
select:{
id:true,
userId:true,
fruitNumber:true,
created: true,
updated: true,
},
where: {
userId: userId
}
}
}
})
return shop;
};
example payload expected:
[
{ id: 1001, name: 'test1', logo: 'log.png', fruit: null },
{ id: 1002, name: 'test2', logo: 'log2.jpg', fruit: null },
{ id: 1003, name: 'test3', logo: 'log3.jpg', fruit: null },
{
id: 1005,
name: 'test4',
logo: 'log4.png',
fruit: {
id: '62450ee5-e75d-4a67-8d79-120d11ddf508',
userId: 111,
fruitNumber: '123456',
created: 2022-07-01T06:39:52.924Z,
updated: 2022-07-01T06:39:52.936Z
}
},
{
id: 1004,
name: 'test5',
logo: 'log5.jpg',
fruit: {
id: '20e9af37-2e6f-4070-8475-c5a914f311dc',
userId: 111,
fruitNumber: '123878',
created: 2022-07-01T07:21:27.898Z,
updated: 2022-07-01T07:21:27.901Z
}
}
]
I can easily achieve the expected output by not having the "where" but I need it because the fruit object can contain more than 1 object so I need to filter by userId e.g.
fruit: {
id: '62450ee5-e75d-4a67-8d79-120d11ddf508',
userId: 111,
fruitNumber: '123456',
created: 2022-07-01T06:39:52.924Z,
updated: 2022-07-01T06:39:52.936Z
},
{
id: '62450ee5-e75d-4a67-8d79-120d11ddf508',
userId: 999,
fruitNumber: '123456',
created: 2022-07-01T06:39:52.924Z,
updated: 2022-07-01T06:39:52.936Z
}
For the fruit object I need nulls and anything that matches the userId and based on design it should only ever be 1 record for each shop for the specific user.
At somepoint my code seemed to work but after I did a prisma generate it stopped working. Is there another way I can achieve the same result or is there someway to fix this?
Note:version info below.
model UserFruit {
id String @id @default(uuid())
fruitNumber String @map("fruit_number")
shopId Int @unique @map("shop_id")
userId Int @map("user_id")
created DateTime @default(now())
updated DateTime @updatedAt
fruit Fruit @relation(fields: [fruitId], references: [id])
@@unique([userId, fruitId], name: "userFruit")
@@map("user_Fruit")
}
model Shop {
id Int @id @default(autoincrement())
name String @unique
logo String
created DateTime @default(now())
updated DateTime @updatedAt
fruit UserFruit?
@@map("Shop")
}
model User {
id Int @id @default(autoincrement())
created DateTime @default(now())
updated DateTime @updatedAt
uid String @unique
email String @unique
phone String @unique
firstName String @map("first_name")
lastName String @map("last_name")
dob DateTime?
gender String?
roleId Int @default(1) @map("role_id")
role Role @relation(fields: [roleId], references: [id])
@@map("user")
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不进行嵌套
,其中
在顶级仅搜索您需要的水果userId
而不是进行嵌套选择
的商店?它应该使您的查询更简单,并解决您的问题。此查询应输出一系列商店,其中连接的
水果
模型的userId
属性等于您的userId
varible。Why not do a nested
where
at the top level to only search for shops whose fruit has theuserId
you need, rather than doing a nestedselect
? It should make your query simpler and also solve your problem.This query should output an array of shops where the connected
fruit
model'suserId
property equals youruserId
variable.