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.
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'
}
});
Este artigo é coletado da Internet.
Se houver alguma infração, entre em [email protected] Delete.
deixe-me dizer algumas palavras