Prisma 2 -Unkown arg'其中'在select.fruit。您的意思是select&#x27 ;?可用的Args

发布于 2025-02-12 07:32:20 字数 3487 浏览 0 评论 0原文

试图查询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.

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阿楠 2025-02-19 07:32:20

为什么不进行嵌套,其中在顶级仅搜索您需要的水果userId而不是进行嵌套选择的商店?它应该使您的查询更简单,并解决您的问题。

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,
      },
      // Removed the nested "where" from here
    },
  },
  where: {
    // One of the following conditions must be true
    OR: [
      // Return shops who have a connected fruit AND
      // the fruit's "userId" attribute equals the variable "userID"
      {
        fruit: {
          is: {
            userId: userId,
            // Can also simplify this to the below line if you want
            // userId
          },
        },
      },
      // Return shops who do not have a connected fruit
      // this will be true if "fruitId" is null
      // could also write this as {fruit: {is: {}}}
      {
        fruitId: {
          equals: null,
        },
      },
    ],
  },
});

此查询应输出一系列商店,其中连接的水果模型的userId属性等于您的userId varible。

Why not do a nested where at the top level to only search for shops whose fruit has the userId you need, rather than doing a nested select? It should make your query simpler and also solve your problem.

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,
      },
      // Removed the nested "where" from here
    },
  },
  where: {
    // One of the following conditions must be true
    OR: [
      // Return shops who have a connected fruit AND
      // the fruit's "userId" attribute equals the variable "userID"
      {
        fruit: {
          is: {
            userId: userId,
            // Can also simplify this to the below line if you want
            // userId
          },
        },
      },
      // Return shops who do not have a connected fruit
      // this will be true if "fruitId" is null
      // could also write this as {fruit: {is: {}}}
      {
        fruitId: {
          equals: null,
        },
      },
    ],
  },
});

This query should output an array of shops where the connected fruit model's userId property equals your userId variable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文