Types of property 'XXXX' are incompatible

Viewed 269

I am currently using nest js with prisma as orm.

My problem occurs at the time of update , I have the following error the error is recurrent on all the entities which have a relationship with another. only in update.:

TS2322: Type 'UpdateMarketDto' is not assignable to type '(Without<MarketsUpdateInput, MarketsUncheckedUpdateInput> & MarketsUncheckedUpdateInput) | (Without<...> & MarketsUpdateInput)'.   Type 'UpdateMarketDto' is not assignable to type 'Without<MarketsUncheckedUpdateInput, MarketsUpdateInput> & MarketsUpdateInput'.     Types of property 'univer' are incompatible.       Type 'CreateUniverDto | UpdateUniverDto' is not assignable to type 'UniversUpdateOneRequiredWithoutMarketsNestedInput'.         Type 'CreateUniverDto' has no properties in common with type 'UniversUpdateOneRequiredWithoutMarketsNestedInput'.

This is the endpoint :

update(id: number, updateMarketDto: UpdateMarketDto) {
    return this.prisma.markets.update({
      where: { id },
      data: updateMarketDto,
    });
  }

This is the createDTO :

export class CreateMarketDto {
  @ApiProperty({ example: 'Femme' })
  @IsString()
  @IsDefined()
  name: string;

  @ApiProperty({ example: 'textile.png' })
  @IsString()
  @IsOptional()
  icon: string;

  @ApiProperty({ example: 'true' })
  @IsBoolean()
  isActive: boolean;

  @ApiProperty({ example: '3' })
  @IsOptional()
  univer: CreateUniverDto | UpdateUniverDto;
}

This is the updateDTO :

export class UpdateMarketDto extends PartialType(CreateMarketDto) {}

This is prisma model :

model Univers {
    id  Int    @id @default(autoincrement())
    name String
    icon String?
    isActive Boolean    @default(false)
    markets Markets[]
}

model Markets {
    id  Int    @id @default(autoincrement())
    name String
    icon String?
    isActive Boolean    @default(false)
    univer Univers @relation(fields:[univerId], references: [id])
    univerId Int
    categories Categories[]
}
1 Answers

The error you posted looks like a TypeScript error, so it shouldn't actually stop the update from happening unless the error is stopping the server from running.

The error is pretty descriptive -

Type 'CreateUniverDto' has no properties in common with type 'UniversUpdateOneRequiredWithoutMarketsNestedInput'.

Whatever type CreateUniverDto is, it's not compatible with Prisma's UniversUpdateOneRequiredWithoutMarketsNestedInput type. If you're confident that these two types are indeed identical, you can cast it as the desired type:

update(id: number, updateMarketDto: UpdateMarketDto) {
  return this.prisma.markets.update({
    where: { id },  
    data: updateMarketDto as Prisma.UniversUpdateOneRequiredWithoutMarketsNestedInput,
  });
}

You should inspect the Prisma.UniversUpdateOneRequiredWithoutMarketsNestedInput to make sure that its type is exactly what you're passing as updateMarketDto, otherwise the update will fail.

If the two types do not overlap, you need to then provide them to Prisma manually:

update(id: number, updateMarketDto: UpdateMarketDto) {
  return this.prisma.markets.update({
    where: { id },  
    data: {
      isActive: {
        set: updateMarketDto.isActive
      },
      name: {
        set: updateMarketDto.name
      },
      // add other fields here based on where they live in updateMarketDto
    }
  });
}
Related