Skip to content

Cyclic dependency checker should consider different 'as' parameter #1717

Closed

Description

@mlegenhausen

I have following example:

var Sequelize = require('sequelize');
var sequelize = new Sequelize('sequelize-test', 'root', '');
var Content = sequelize.define('Content', {});
var Version = sequelize.define('Version', {});

Content.hasMany(Version);
Version.belongsTo(Content);

Content.belongsTo(Version, {
    as: 'ReleaseVersion',
    foreignKey: 'ReleaseVersionId'
});

Version.hasOne(Content, {
    as: 'ReleaseVersion',
    foreignKey: 'ReleaseVersionId'
});

sequelize.sync({ force: true });

Which produces following error:

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?

Activity

  1. janmeier commented on May 7, 2014

    @janmeier
    Member

    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

  2. mlegenhausen commented on May 7, 2014

    @mlegenhausen
    ContributorAuthor

    Thanks for this super fast response time. That solved the problem.

  3. asimsohail commented on Jul 1, 2015

    @asimsohail

    @janmeier.. can you show how to manually add the keys after .sync is complete?

  4. janmeier commented on Jul 2, 2015

    @janmeier
    Member

    @asimsohail Add them manually either using phpmyadmin/pgadmin whatever or by writing the query

  5. bottleofwater commented on Oct 30, 2015

    @bottleofwater

    @janmeier Couldn't Sequelize first create the tables without the foreign keys, then add all the foreign keys right after?

  6. mickhansen commented on Oct 31, 2015

    @mickhansen
    Contributor

    @bottleofwater that could work

  7. markushedlund commented on Mar 14, 2016

    @markushedlund
    Contributor

    Is constraints: false and adding keys after .sync the canonical way of doing cyclic database structures?

    Table structure:

    template
    - id
    - category_id
    
    category
    - id
    - default_template_id
    
  8. ralusek commented on Dec 15, 2016

    @ralusek

    @mickhansen Is this currently in development?

  9. marc-ferrer commented on Jan 30, 2017

    @marc-ferrer

    I know this is an old issue, but why does not allowNull: true on the foreign key parameter make this work?

  10. jasonbodily commented on Mar 8, 2017

    @jasonbodily

    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:

    1. Add 'constraints: false' on one of the associations
    2. Remove the relation information from the model attributes altogether
    3. 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')
    
  11. gzurbach commented on Mar 14, 2017

    @gzurbach

    @jasonbodily it's constraints: false (with an s)... spent a few minutes figuring that out :)

  12. jasonbodily commented on Mar 14, 2017

    @jasonbodily

    @gzurbach - Sorry about that! Updated my answer!

  13. mentos1386 commented on Apr 11, 2017

    @mentos1386

    @jasonbodily You have a typo, constrainst: false should be constraints: false (ts at the end`)

  14. petersg83 commented on Oct 27, 2017

    @petersg83

    Same problem here.
    More than 2 years this issue has been opened :)
    I'll add contraints: false but it hurts my heart :'(

  15. 4 remaining items

  16. added
    type: feature
    on Oct 18, 2018
  17. yocontra commented on Oct 18, 2018

    @yocontra
    Contributor
  18. petrovi4 commented on Jan 8, 2019

    @petrovi4

    @sushantdhiman Is this in development now?

  19. stale commented on Jul 23, 2019

    @stale
  20. LFabre commented on Jan 15, 2020

    @LFabre

    Any news?

  21. papb commented on Jan 16, 2020

    @papb
    Member

    @LFabre and others - none that I know of. PR is welcome.

  22. added
    status: understood
    on Jan 16, 2020
  23. esyeng commented on Sep 11, 2020

    @esyeng

    Any news? :-(

  24. github-actions commented on May 23, 2022

    @github-actions
    Contributor

    🎉 This issue has been resolved in version 6.20.0 🎉

    The release is available on:

    Your semantic-release bot 📦🚀

  25. ephys commented on May 23, 2022

    @ephys
    Member

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    releasedstatus: understoodtype: feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions