对象字面量只能指定已知的属性,并且类型 'RatesWhereUniqueInput' 中不存在 'clientId' 解决方案

问题描述

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

在我的存储库中,我想使用这两个来使用“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,});
    }
}

解决方法

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

解决方案

尝试按如下方式更改 update 查询:

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

这是 Prisma 在为具有复合 ID 或唯一标识符(由模型的多个字段组成的 ID 或唯一标识符)的模型指定 where 条件时使用的语法。您可以在 Prisma 文档中 CRUD Reference Guide通过复合 ID 或复合唯一标识符获取记录小节中阅读更多内容。