【问题标题】:Prisma findMany failingPrisma findMany失败
【发布时间】:2021-06-15 10:15:35
【问题描述】:

最近我将 prisma 从 2.7.0 更新到 2.19.0,我分别进行了更改(主要是 findOne 到 findUnique)但是这个拒绝工作:

const eventFilter = { AND: [
    { startTime: { lte: moment().toISOString() } },
    { endTime: { gte: moment().toISOString() } },
    { place:{ id: placeId || { not: null } } }
 ] }

await prisma.eventParticipant.findMany({
    where: {
        user: { id: XXX },
        event: eventFilter
    }
})

但是这样做会引发这个错误(短版呵呵): Unknown arg `AND` in where.event.AND for type EventRelationFilter

所以我在index.d.ts(在node_modules/.prisma/client)寻找findMany(...)参数的类型,我发现:

findMany<T extends EventParticipantFindManyArgs>(
      args?: SelectSubset<T, EventParticipantFindManyArgs>
    ): CheckSelect<T, PrismaPromise<Array<EventParticipant>>, PrismaPromise<Array<EventParticipantGetPayload<T>>>>

然后我寻找 EventParticipantFindManyArgs 有什么:

export type EventParticipantFindManyArgs {
    // Has another props but this one is that interest
    where?: EventParticipantWhereInput
}

再一次...寻找EventParticipantWhereInput 有什么:

export type EventParticipatnWhereInput {
    // Like before, this one has another props but this one is that interest
    event?: XOR<EventRelationFilter, EventWhereInput> | null
}

此时我预计eventFilter(我的常量)适合EventParticipatnWhereInput.event 类型

最后我找到了EventRelationFilter,它有:

export type EventRelationFilter = {
    is?: EventWhereInput | null
    isNot?: EventWhereInput | null
}

Aaand EventWhereInput 拥有 eventFilter 拥有的所有道具

所以......当我看到它时,我说:“为什么我的 eventFilter 对象不适合 EventWhereInput?”

我试图解决这个问题:

import { Prisma } from '@prisma/client' // Import types from exported namespace called "Prisma"

// This one doesn't throw error like "{...} is not assignable to object of type Prisma.EventWhereInput
// So I assume this is fine
const eventFilter: Prisma.EventWhereInput = { AND: [
    { startTime: { lte: moment().toISOString() } },
    { endTime: { gte: moment().toISOString() } },
    { place:{ id: placeId || { not: null } } }
 ] }

但是!这样做没有任何改变,和以前一样的错误(prisma 期望 where.event 是“EventRelationFilter”类型)

所以我说“好的你的fking棱镜,我会让eventFilter“EventRelationFilter”类型:

import { Prisma } from '@prisma/client' // Import types from exported namespace called "Prisma"

// This one doesn't throw error like "{...} is not assignable to object of type Prisma.EventRelationFilter
// So I assume this is fine too
const eventFilter: Prisma.EventRelationFilter = { is: { AND: [
    { startTime: { lte: moment().toISOString() } },
    { endTime: { gte: moment().toISOString() } },
    { place:{ id: placeId || { not: null } } }
 ] } }

但是(从这一点开始,我的希望已经转移到 stackoverflow 上)抛出这个错误: Unknown arg `is` in where.event.is for type EventWhereInput

我说:“什么?!两者都是错误?棱镜想从我这里得到什么?”

对不起,我的问题很长,但我对此很困惑。

顺便说一句:eventParticipants 是用户和事件之间的“连接表”我不确定????

编辑:架构:

model Event {
  id               Int                @id @default(autoincrement())
  name             String
  description      String?
  // ... 
  participantUsers EventParticipant[]
}


model User {
  id                        Int                    @id @default(autoincrement())
  name                      String
  alias                     String?                @unique @default(dbgenerated())
  birthDay                  DateTime?              @map("dateOfBirth")
  // ...                    
  event                     Event[]
  @@map(name: "user")
}

model EventParticipant {
  id       Int      @id @default(autoincrement())
  event_id Int?
  user_id  Int?
  event    Event?   @relation(fields: [event_id], references: [id])
  user     User?    @relation("event_participants_idToUser", fields: [user_id], references: [id])
  @@map(name: "event_participants")
}

谢谢!

【问题讨论】:

  • 您能分享一下架构吗?

标签: javascript node.js typescript prisma


【解决方案1】:

抱歉,我的错误...place: { id: { not: null } }“not”期望有 NestedIntFilternumber,因此 null 是该键的无效值

const eventFilter: Prisma.EventWhereInput = { AND: [
    { startTime: { lte: moment().toISOString() } },
    { endTime: { gte: moment().toISOString() } },
    // WRONG { place:{ id: placeId || { not: null } } }
    { place:{ id: placeId } } // FINE!
 ] }

需要以另一种方式修复该验证。奇怪的是,我的 IDE 没有抛出像 {...} is not assignable to Prisma.EventWhereInput 或类似的错误。

【讨论】:

    猜你喜欢
    • 2021-09-13
    • 2021-10-07
    • 2022-08-04
    • 2020-11-07
    • 2021-07-24
    • 2022-01-10
    • 2022-01-17
    • 2022-09-27
    • 2019-12-04
    相关资源
    最近更新 更多