You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Cyclic dependency checker should consider different 'as' parameter #1717
Error: Cyclic dependency found. 'Contents' is dependent of itself.
Dependency Chain: Contents -> Versions => Contents
I want to associate the active release of a content by the Content model, for this I added a new association that links directly to my Version model. The key for this id (ReleaseVersionId) should be in the Content Table. This key can be null if no version was released. Since Version 2.0.0-dev11 I get an error when constructing something like this. Do I miss something or can I model something like this in a sequelize way?
Reacted by Ilia Sidorenko, Pierre Noël, Pedro Augusto de Paula Barbosa, Nate, Andreas W Johansson, petrovi4, Abdullah Saleem, Josiel Faleiros Alves, Ivan Ilinykh and Simon Chaumet
This is because dev11 introduces automatic foreign keys on your tables. This means that contentId on version will reference content.id, and content.realeaseVersionId will reference version.id. Since both tables now depend on each other, we cannot create any of them (content must be created before version, and version must be created before content.
To fix this problem, you should add constraints: false to the options of one or more of your associations to disable the foreign keys for these. If you want the security of foreign keys, you could add the keys manually after .sync has finished
Reacted by Tanveer Singh
Reacted by Josiel Faleiros Alves, Kein-Wai, Binit-bestone and hokin2023
@janmeier Couldn't Sequelize first create the tables without the foreign keys, then add all the foreign keys right after?
Reacted by Emanuele Bertoldi, Armando Padilla, Andrey Balakirev, Ante Barišić, Kristijan Trajkovski, Bartek Paczkowski, mhoffmann, Alexander Dransfield, recurrence, Jon Perl and 16 more
Yes old issue, but it doesn't appear to work. With above, is this currently available?
EDIT: This does kind of work. You just have to do 3 things:
Add 'constraints: false' on one of the associations
Remove the relation information from the model attributes altogether
Write the custom migration that adds the fkey at the appropriate time. An example of that custom query is
queryInterface.sequelize.query('ALTER TABLE public."<source_table>" ADD CONSTRAINT
"<source_table>_<local_fkey>_fkey" FOREIGN KEY (<local_fkey>)
REFERENCES public."<TargetTable>" (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION')
I'll close. Starting with Sequelize 6.20.0, if there is a cyclic foreign key, sync will first create the tables without foreign key constrants, then add them.
This change should resolve this issue.
Activity
janmeier commentedon May 7, 2014
This is because dev11 introduces automatic foreign keys on your tables. This means that contentId on version will reference content.id, and content.realeaseVersionId will reference version.id. Since both tables now depend on each other, we cannot create any of them (content must be created before version, and version must be created before content.
To fix this problem, you should add
constraints: falseto the options of one or more of your associations to disable the foreign keys for these. If you want the security of foreign keys, you could add the keys manually after.synchas finishedmlegenhausen commentedon May 7, 2014
Thanks for this super fast response time. That solved the problem.
asimsohail commentedon Jul 1, 2015
@janmeier.. can you show how to manually add the keys after
.syncis complete?janmeier commentedon Jul 2, 2015
@asimsohail Add them manually either using phpmyadmin/pgadmin whatever or by writing the query
bottleofwater commentedon Oct 30, 2015
@janmeier Couldn't Sequelize first create the tables without the foreign keys, then add all the foreign keys right after?
mickhansen commentedon Oct 31, 2015
@bottleofwater that could work
markushedlund commentedon Mar 14, 2016
Is
constraints: falseand adding keys after.syncthe canonical way of doing cyclic database structures?Table structure:
ralusek commentedon Dec 15, 2016
@mickhansen Is this currently in development?
marc-ferrer commentedon Jan 30, 2017
I know this is an old issue, but why does not
allowNull: trueon the foreign key parameter make this work?jasonbodily commentedon Mar 8, 2017
Yes old issue, but it doesn't appear to work. With above, is this currently available?
EDIT: This does kind of work. You just have to do 3 things:
gzurbach commentedon Mar 14, 2017
@jasonbodily it's
constraints: false(with ans)... spent a few minutes figuring that out :)jasonbodily commentedon Mar 14, 2017
@gzurbach - Sorry about that! Updated my answer!
mentos1386 commentedon Apr 11, 2017
@jasonbodily You have a typo,
constrainst: falseshould beconstraints: false(tsat the end`)petersg83 commentedon Oct 27, 2017
Same problem here.
More than 2 years this issue has been opened :)
I'll add
contraints: falsebut it hurts my heart :'(4 remaining items
yocontra commentedon Oct 18, 2018
@sushantdhiman Thanks!
petrovi4 commentedon Jan 8, 2019
@sushantdhiman Is this in development now?
stale commentedon Jul 23, 2019
LFabre commentedon Jan 15, 2020
Any news?
papb commentedon Jan 16, 2020
@LFabre and others - none that I know of. PR is welcome.
esyeng commentedon Sep 11, 2020
Any news? :-(
github-actions commentedon May 23, 2022
🎉 This issue has been resolved in version 6.20.0 🎉
The release is available on:
Your semantic-release bot 📦🚀
ephys commentedon May 23, 2022
I'll close. Starting with Sequelize 6.20.0, if there is a cyclic foreign key,
syncwill first create the tables without foreign key constrants, then add them.This change should resolve this issue.