gpt4 book ai didi

node.js - 依赖链 : category -> shop => category in model sequelizejs while defining foreign key

转载 作者:搜寻专家 更新时间:2023-10-31 23:48:04 25 4
gpt4 key购买 nike

错误列表:

\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);

从 category -> shop 创建关系,而 scategory 列从 shop -> category 创建关系。因为关系是双向的,所以 sequelize 不知道先创建哪个表 - 两个表都需要先创建另一个表。

您可能需要反转关系到:

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

一个类别是否只与一家商店有关?否则,您需要 Category.hasMany(shop); 来使同一类别与多个商店相关。

此外,您不需要既添加列(分类)又调用关联函数—​​—一个就足够了。这应该足够了:

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

同时从商店的定义中删除 scategory。要强制 scategory 不能为 null,您可以执行以下操作:

Shop.belongsTo(Category, { 
foreignKey: {
allowNull: false,
name: 'scategory'
}
});

关于node.js - 依赖链 : category -> shop => category in model sequelizejs while defining foreign key,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29249133/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com