I have this typeorm model with a composite unique index:
@Entity({name: "positions"})
@Unique("constraint_unique_position", ["role_id", "organisation_id", "soato"])
export class Position {
@PrimaryGeneratedColumn()
id: number
@Column()
role_id: number
@OneToOne(() => Role)
@JoinColumn({name: "role_id"})
role: Role;
@Column()
organisation_id: number
@OneToOne(() => Organisation)
@JoinColumn({name: "organisation_id"})
organisation: Organisation;
@Column()
@MaxLength(7, {message: "soato must be 7 characters maximum"})
soato: number
@OneToMany(() => PositionPermissions, (perm:PositionPermissions) => perm.position)
permissions: PositionPermissions[]
}
As you can see role_id
, organisation_id
, soato
make a composite unique index. But when I inspect the generated table, along with the composite unique index each of these column have their own unique index:
As a result, I can’t insert a value twice to a column:
INSERT INTO positions (
role_id,
organisation_id,
soato,
status
) VALUES (
5, 2, 17
), (
4, 2, 17
);
This gives error as value 2 is already inserted to organisation_id
.
Is my table wrong?
How can I achieve to only have the composite unique index created?