【问题标题】:Object literal may only specify known properties, and 'clientId' does not exist in type 'RatesWhereUniqueInput'对象字面量只能指定已知属性,并且“RatesWhereUniqueInput”类型中不存在“clientId”
【发布时间】:2021-09-27 09:05:30
【问题描述】:

我正在使用带有 prisma 和 typegraphql 的 typescript 并收到此类型错误。 RatesWhereUniqeInput 由 prisma 生成,并将自身定义为“CompoundUniqueInput”,因为我引用的数据库有 2 个键(clientId:string,employeeId:number)。

在我的存储库中,我想同时使用这两种方法来使用“update()”来引用特定的数据库行,但是因为类型是在 prisma 客户端中生成的 clientId_employeeId?我收到类型错误。

存储库函数

    async update(model: Rates): Promise<Rates> {
        const { employeeId, clientId, ...data } = this.mapper.from(model);

        const entity = await this.db.rates.update({
            where: { employeeId, clientId },
            data: {
                ...data,
            },
        });

        return this.mapper.to(entity);
    }

棱镜索引.d.ts

  export type RatesWhereUniqueInput = {
    clientId_employeeId?: RatesClientIdEmployeeIdCompoundUniqueInput
  }

ratesmapper.ts

@injectable()
export class RatesMapper {
    public from(model: Rates): RatesEntity {
        return {
            employeeId: model.employeeId,
            clientId: model.clientId,
            rateFull: model.rateFull,
            rateQuarter: model.rateQuarter,
            rateLine: model.rateLine,
            rateWord: model.rateWord,
            createdAt: model.createdAt,
            updatedAt: model.updatedAt,
        };
    }

    public to(entity: RatesEntity): Rates {
        return new Rates({
            ...entity,
        });
    }
}

【问题讨论】:

    标签: typescript apollo prisma typegraphql


    【解决方案1】:

    澄清一下,您正在尝试使用 prisma 对来自 Rates 表/模型的单个记录运行 update 查询。为此,您的where 条件中出现错误。

    解决方案

    尝试如下更改update 查询:

    const entity = await this.db.rates.update({
            where: {
                clientId_employeeId: {
                    employeeId,
                    clientId,
                },
            },
            data: {
                ...data,
            },
        });
    
    

    这是 Prisma 在为具有复合 ID 或唯一标识符(由模型的多个字段组成的 ID 或唯一标识符)的模型指定 where 条件时使用的语法。您可以在 Prisma 文档中CRUD Reference Guideget record by Compound ID or Compound unique identifier 小节中阅读更多内容。

    【讨论】:

    • 你让我很开心!从昨天开始,我阅读了一半以上的文档,一定错过了那部分。
    猜你喜欢
    • 2021-02-18
    • 2017-08-10
    • 1970-01-01
    • 1970-01-01
    • 2020-11-27
    • 1970-01-01
    • 2022-08-24
    • 1970-01-01
    • 2018-09-06
    相关资源
    最近更新 更多