'Unable to create @manyToMany realtion ship in mikro-orm
I have issue with my @manyToMany relationship in mikro-orm framework. I created this entity:
enum NotificationCategory {
REPORT = "report",
PARKING = "parking",
}
@Entity()
export class Notification {
@PrimaryKey()
@Unique()
id!: number;
@Property()
message!: string;
@Property()
date!: Date;
@Property()
seen!: boolean;
@Enum(() => NotificationCategory)
category!: NotificationCategory;
@ManyToMany({
entity: () => User,
})
user?: Collection<User> = new Collection<User>(this);
@ManyToOne({
entity: () => Report,
onUpdateIntegrity: "cascade",
onDelete: "set null",
nullable: true,
primary: true,
})
report?: Report;
@ManyToOne({
entity: () => ParkingLot,
onUpdateIntegrity: "cascade",
onDelete: "set null",
nullable: true,
primary: true,
})
parkingLot?: ParkingLot;
}
After I added user atribute and drop schema and make new migration I am getting this error:
DriverException: alter table "notification_user" add constraint "notification_user_notification_id_notification_report_id_notification_parking_lot_id_foreign" foreign key ("notification_id", "notification_report_id", "notification_parking_lot_id") references "notification" ("id", "report_id", "parking_lot_id") on update cascade on delete cascade; - there is no unique constraint matching given keys for referenced table "notification"
The weird think is that migration which is generated automatically has no contains table notification. Am I doing something wrong or why this is not working please?
Migration file:
this.addSql('create table "notification_user" ("notification_id" int4 not null, "notification_report_id" int4 not null, "notification_parking_lot_id" int4 not null, "user_id" int4 not null);');
this.addSql('alter table "notification_user" add constraint "notification_user_pkey" primary key ("notification_id", "notification_report_id", "notification_parking_lot_id", "user_id");');
this.addSql('alter table "notification_user" add constraint "notification_user_notification_id_notification_report_id_notification_parking_lot_id_foreign" foreign key ("notification_id", "notification_report_id", "notification_parking_lot_id") references "notification" ("id", "report_id", "parking_lot_id") on update cascade on delete cascade;');
this.addSql('alter table "notification_user" add constraint "notification_user_user_id_foreign" foreign key ("user_id") references "user" ("id") on update cascade on delete cascade;');
this.addSql('alter table "notification" add constraint "notification_id_unique" unique ("id");');
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
