One issue I encountered is related to the FK relationship between Group and GroupAttendee. Whenever I try to call Group.destroy()
, a foreign key constraint failure exception regarding the GroupAttendee entries pops up. I know how these constraints work at the database level, but I'm struggling to make sequelize-typescript create or enforce them properly.
Below is a simplified version of my models to showcase the setup:
import { Model, Column, AllowNull, HasMany, ForeignKey, DataType, BelongsTo } from "sequelize-typescript";
import { User } from "./User";
class Group extends Model<Group> {
@AllowNull(false)
@Column
title: string;
@AllowNull(false)
@Column
startDate: Date;
@AllowNull(false)
@Column
endDate: Date;
@HasMany(() => GroupAttendee)
attendees: GroupAttendee[];
}
class GroupAttendee extends Model<GroupAttendee> {
@ForeignKey(() => User)
@Column(DataType.INTEGER)
userId: number;
@ForeignKey(() => Group)
@Column(DataType.INTEGER)
groupId: number;
@BelongsTo(() => Group, {
onUpdate: "CASCADE",
onDelete: "CASCADE",
hooks: true
})
group: Group;
}
Is there anyone who has successfully managed to solve this issue?