Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dependency Chain: category -> shop => category in model sequelizejs while defining foreign key

List of Error:

\Possibly unhandled Error: Cyclic dependency found. 'category' is dependent of itself. Dependency Chain: category -> shop => category
at visit (/home/rashmi/nodejs/node_modules/sequelize/node_modules/toposort-class/toposort.js:74:27)
at /home/rashmi/nodejs/node_modules/sequelize/node_modules/toposort-class/toposort.js:96:25
at Array.forEach (native)
at visit (/home/rashmi/nodejs/node_modules/sequelize/node_modules/toposort-class/toposort.js:95:20)
at /home/rashmi/nodejs/node_modules/sequelize/node_modules/toposort-class/toposort.js:96:25
at Array.forEach (native)
at visit (/home/rashmi/nodejs/node_modules/sequelize/node_modules/toposort-class/toposort.js:95:20)
at Toposort.self.sort (/home/rashmi/nodejs/node_modules/sequelize/node_modules/toposort-class/toposort.js:104:21)
at module.exports.ModelManager.forEachDAO (/home/rashmi/nodejs/node_modules/sequelize/lib/model-manager.js:88:21)
at /home/rashmi/nodejs/node_modules/sequelize/lib/sequelize.js:894:25

While running this file Model.js

var Category=sequelize.define("category",{
    categoryname :{
        type: Sequelize.STRING,
        validate:{isAlpha:true}
    }},
    {
        paranoid: true,
        freezeTableName: true, //modeltable name will be the same as model name
        comment: "I'm Category table!"
    });

var shop=sequelize.define("shop",{
    shopID:{
        type: Sequelize.INTEGER,
        allowNull: false,
        primaryKey: true,
        autoIncrement: true
    },
    title: {
        type: Sequelize.STRING(100),
         allowNull: false,
        validate:{isAlpha: true} 
    },
    shopKeeperName:{
        type: Sequelize.STRING(100),
         allowNull: false,
        validate:{isAlpha: true} 
    },
    mobile :{
        type: Sequelize.CHAR(10),
         allowNull: false
    },
    city :{
        type: Sequelize.INTEGER,
         allowNull: false,
         references: "City",
         referencesKey: "cityId"
    },
    scategory :{
        type: Sequelize.STRING,
        allowNull: false,
        references: "category",
        referencesKey: "Id"
    },
    address :{
        type: Sequelize.TEXT,
        allowNull: false,
        validate:{ isAlphanumeric:true}
    },
    stock :{
        type: Sequelize.INTEGER,
        validate: {isInt: true}
    }
    },
    {
        paranoid: true,
        freezeTableName: true, //modeltable name will be the same as model name
        underscored: true,
        comment: "I'm Shop table!"
    });

state.hasMany(city);
city.belongsTo(state,{foreignKey: 'stateID'});

Agent.hasOne(city);
city.belongsTo(Agent);

Agent.hasOne(state);
state.belongsTo(Agent);

shop.hasOne(Category);
Category.belongsTo(shop);
sequelize.sync();

While defining shop model, I got the above errors. foreign key categoryid in shop, I'm not getting the exact reason, what it means by Shop is dependent on itself, dependency cycle.

like image 467
Rashmi Jain Avatar asked Jan 25 '26 00:01

Rashmi Jain


2 Answers

shop.hasOne(Category);
Category.belongsTo(shop);

Creates a relation from category -> shop, while the scategory column creates a relation from shop -> category. Because the relation goes both ways, sequelize does not know which table to create first - both tables require the other table to be created first.

You probably need to reverse the relation to:

shop.belongsTo(Category);
Category.hasOne(shop);

Does a category only relate to a single shop? Otherwise you need Category.hasMany(shop); to enable the same category to be related to several shops.

Furthermore, you don't need to both add a column (scategory) and call association functions - one is enough. This should be sufficient:

shop.belongsTo(Category, { foreignKey: 'scategory' });

While removing scategory from the defintion of shop. To enforce that scategory cannot be null, you can do:

Shop.belongsTo(Category, { 
  foreignKey: { 
    allowNull: false, 
    name: 'scategory'
  }
});
like image 199
Jan Aagaard Meier Avatar answered Jan 26 '26 15:01

Jan Aagaard Meier


However, the code above will result in the following error: Cyclic dependency found. 'Document' is dependent of itself. Dependency Chain: Document -> Version => Document. In order to alleviate that, we can pass constraints: false to one of the associations: Document.hasMany(Version) Document.belongsTo(Version, { as: 'Current', foreignKey: 'current_version_id', constraints: false})

http://docs.sequelizejs.com/en/latest/docs/associations/

like image 27
Dickeylth Avatar answered Jan 26 '26 14:01

Dickeylth