postgresqlnestjsrelationmikro-orm

Mikro ORM unique decorator is not being enforced when


Using this entity:

@Entity()
@Unique({ properties: ['case', 'csgoItem'] })
export class CaseItem {
  @PrimaryKey({ type: 'uuid', defaultRaw: 'uuid_generate_v4()' })
  id: string;

  @Property({ type: 'double precision', columnType: 'double precision' })
  probability: number;

  @ManyToOne(() => Case)
  case!: Case;

  @ManyToOne(() => CsgoItem, { cascade: [Cascade.REMOVE] })
  csgoItem!: CsgoItem;
}

When trying to insert a duplicate (based on the combination of case and csgItem, mikroORM allows me to do this. How can I make the @unique decorator work with relations?


Solution

  • This is a schema feature, the ORM does not enforce it anyhow, so you probably have out-of-sync schema - in other words, the unique constraint is not created.

    Try running npx mikro-orm schema:update --run, or even better, adopt migrations if it's something more than just a toy project.

    Few notes about the code you provided: