Prisma findMany 查询条件为关联字段不为空的情况。

huangapple go评论92阅读模式
英文:

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,
  });

huangapple
  • 本文由 发表于 2023年7月27日 17:06:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76778166.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定