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?
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:
probability
is double, so it will be mapped to string
, not number
, as you can't fit a double into JS numbertype
and columnType
together, not when you provide the same value in therecascade: [Cascade.REMOVE]
is probably not doing what you think, that is app-level cascading and means that when you delete the owning entity (CaseItem), its relations (marked with Cascade.REMOVE
) will be removed too - this is the opposite of onDelete: 'cascade'
, which I guess you want instead (so when the target is removed - the CsgoItem
entity - the owner (so Case
) will be removed too