英文:
Prisma findMany where relationship is not null
问题
我有这两个低端模型,我想列出它们所有的子类别及其菜单,前提是菜单不为空。
model SubCategory {
id Int @id @default(autoincrement())
label String
image String
categories Category[] @relation("CategoryToSubCategory")
menu Menu[] @relation("MenuToSubCategory")
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
model Menu {
id Int @id @default(autoincrement())
name String @db.VarChar(64)
description String @db.Text
favoriteMenus FavoriteMenus[]
ingredients Ingredients[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@index([chefStoreId], map: "Menu_chefStoreId_fkey")
@@fulltext([name, description])
}
我正在编写的查询如下:
const topCategories = await this.prisma.subCategory.findMany({
include: {
menu: {
where: {
id: { not: null },
},
},
},
orderBy: {
id: 'desc',
},
take: 50,
});
但是它给我报错了,错误信息是"Argument
not must not be null."
英文:
I have these two low-end models and I want to list all their subcategories with their menus, provided that the menu is not empty.
model SubCategory {
id Int @id @default(autoincrement())
label String
image String
categories Category[] @relation("CategoryToSubCategory")
menu Menu[] @relation("MenuToSubCategory")
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
model Menu {
id Int @id @default(autoincrement())
name String @db.VarChar(64)
description String @db.Text
favoriteMenus FavoriteMenus[]
ingredients Ingredients[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@index([chefStoreId], map: "Menu_chefStoreId_fkey")
@@fulltext([name, description])
}
The query I am writing is as follows:
const topCategories = await this.prisma.subCategory.findMany({
include: {
menu: {
where: {
id: { not: null },
},
},
},
orderBy: {
id: 'desc',
},
take: 50,
});
But it gives me the error "Argument
not must not be null."
答案1
得分: 0
我的翻译如下:
我的意思是,menu
永远不会为空,因为它是一个数组,它只会是一个空数组。在你的模式中,Menu
的id也不可能为空。
但是,如果你的意思是要找到至少有一个关联菜单的subCategory
,那么我认为你需要使用带有空参数的some
关系过滤器(文档):
const topCategories = this.prisma.subCategory.findMany({
where: {
menu: {
some: {},
},
},
include: {
menu: true,
},
orderBy: {
id: 'desc',
},
take: 50,
});
英文:
I mean, menu
won't ever be null because it's an array, it will be just an empty array. Menu
id also can't be null in your schema.
But if you mean that you want to find subCategory
which has at least 1 menu attached, then I think you need to use some
relation filter with empty params (docs):
const topCategories = this.prisma.subCategory.findMany({
where: {
menu: {
some: {},
},
},
include: {
menu: true,
},
orderBy: {
id: 'desc',
},
take: 50,
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论