依赖链:定义外键时模型 sequelizejs 中的类别 -> 商店 => 类别
Dependency Chain: category -> shop => category in model sequelizejs while defining foreign key
错误列表:
\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
同时 运行 这个文件 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();
在定义商店模型时,我遇到了上述错误。 shop中的外键categoryid,我没有得到确切的原因,Shop是什么意思依赖它自己,依赖循环。
shop.hasOne(Category);
Category.belongsTo(shop);
从类别 -> 商店创建关系,而 scategory
列从商店 -> 类别创建关系。因为关系是双向的,所以 sequelize 不知道先创建哪个 table - 两个 table 都需要先创建其他 table。
您可能需要反转关系到:
shop.belongsTo(Category);
Category.hasOne(shop);
一个类别是否只与一家商店有关?否则您需要Category.hasMany(shop);
才能使同一类别与多个商店相关。
此外,您不需要既添加列(分类)又调用关联函数——一个就够了。这应该足够了:
shop.belongsTo(Category, { foreignKey: 'scategory' });
同时从商店定义中删除 scategory
。要强制 scategory
不能为空,您可以执行以下操作:
Shop.belongsTo(Category, {
foreignKey: {
allowNull: false,
name: 'scategory'
}
});
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})
错误列表:
\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
同时 运行 这个文件 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();
在定义商店模型时,我遇到了上述错误。 shop中的外键categoryid,我没有得到确切的原因,Shop是什么意思依赖它自己,依赖循环。
shop.hasOne(Category);
Category.belongsTo(shop);
从类别 -> 商店创建关系,而 scategory
列从商店 -> 类别创建关系。因为关系是双向的,所以 sequelize 不知道先创建哪个 table - 两个 table 都需要先创建其他 table。
您可能需要反转关系到:
shop.belongsTo(Category);
Category.hasOne(shop);
一个类别是否只与一家商店有关?否则您需要Category.hasMany(shop);
才能使同一类别与多个商店相关。
此外,您不需要既添加列(分类)又调用关联函数——一个就够了。这应该足够了:
shop.belongsTo(Category, { foreignKey: 'scategory' });
同时从商店定义中删除 scategory
。要强制 scategory
不能为空,您可以执行以下操作:
Shop.belongsTo(Category, {
foreignKey: {
allowNull: false,
name: 'scategory'
}
});
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})