Foreign keys hell

18 views
Skip to first unread message

Michal

unread,
Apr 25, 2023, 4:43:32 AM4/25/23
to Sequelize
First of all - I am experienced Sequelize user and struggle a lot with sequelize documentation. Anyway I have several use-cases and need some if not "how-it-works" at least "the-intenation-was-this"...

BelongsTo A > BelongsTo B > BelongsTo A
I have model A and model B and what to create following foreign keys:

Parent.belongsTo(Child, {foreignKey: TallestChildId})
Parent.belongsTo(Child, {foreignKey: FavouriteChildId})
Child.belongsTo(Parent, {foreignKey: ParentId})

this will obviously produces circular dependency howeve using constraints: false does not work well here as all of these foreign keys must be created. Is there any hidden trick or I have to migrate?

One bonus question:

Is it possible to create foreign key without onDelete or onUpdate rule? as there are default values it is not possible (to my understanding and also to my tests) to generate following commands:

ALTER TABLE table1 ADD CONSTRAINT table1_column1_fkey FOREIGN KEY (column1) REFERENCES table2(id) ON DELETE RESTRICT;

or 

ALTER TABLE table1 ADD CONSTRAINT table1_column1_fkey FOREIGN KEY (column1) REFERENCES table2(id) ON UPDATE SET NULL;

Thanks for any response
Mike
Reply all
Reply to author
Forward
0 new messages