错误列表:
\可能未处理错误:找到循环依赖项。 '类别'是 依赖于自己。依赖链:类别 - > shop =>类别
在访问 (/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
在Array.forEach(native)
在访问时(/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
在Array.forEach(native)
在访问时(/home/rashmi/nodejs/node_modules/sequelize/node_modules/toposort-class/toposort.js:95:20)
在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();
在定义商店模型时,我遇到了上述错误。商店中的外键classid,我没有得到确切的理由,Shop的含义取决于它自身,依赖循环。
答案 0 :(得分:2)
但是,上面的代码将导致以下错误:找到循环依赖项。 '文件'依赖于自己。依赖链:文档 - >版本=>文献。为了减轻这种情况,我们可以将约束传递给false:其中一个关联:
Document.hasMany(Version) Document.belongsTo(Version, { as: 'Current', foreignKey: 'current_version_id', constraints: false})
答案 1 :(得分:1)
shop.hasOne(Category);
Category.belongsTo(shop);
从类别创建关系 - >购物,而scategory
列从商店创建关系 - >类别。因为关系是双向的,所以sequelize不知道首先要创建哪个表 - 两个表都需要先创建另一个表。
您可能需要将关系转换为:
shop.belongsTo(Category);
Category.hasOne(shop);
某个类别仅与单个商店相关吗?否则,您需要Category.hasMany(shop);
才能使同一类别与多家商店相关。
此外,您不需要添加列(scategory)和调用关联函数 - 一个就足够了。这应该足够了:
shop.belongsTo(Category, { foreignKey: 'scategory' });
从商店的定义中移除scategory
。要强制scategory
不能为null,您可以执行以下操作:
Shop.belongsTo(Category, {
foreignKey: {
allowNull: false,
name: 'scategory'
}
});