我正在将sequelize v5.21和sequelize-cli v5.5.1用于MySQL数据库。
我目前正在尝试在以下模型下创建一些1:M关联,该模型是从cli生成的代码,如下所示:
其中:a是源,b是目标。
model.associate = function (models) {
// associations can be defined here
a.hasOne(models.b);
}
和其他模型,我有:
model.associate = function (models) {
// associations can be defined here
b.belongsTo(models.a);
}
最后,即时消息使用此代码通过创建条目来检查关联:
a.create({
...
}).then(a => {
return a.createB({
...,
}).catch(err => console.log(err));
}).catch(err => console.log(err)
);
我在“ a.createB()...”上遇到错误,因为它不是函数...
所以,我很好奇,在检查关联之前尝试进行关联:
a.hasOne(b);
b.belongsTo(a);
在其中工作得很好...
我的问题是,“ model.associate”属性是否仍适用于v5 ^?
注意:我已经使用了sequelize文档中提供的检查关联方法,但是我更喜欢这一方法,因为它更易于阅读。
答案 0 :(得分:0)
在v5的Sequelize文档中,我没有找到关于model.associate
的任何信息,但是在我的项目中,我在主要模型目录中使用了以下代码:
Object.keys(db).forEach(modelName => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});
其中db
由sequelize['import']
导入的sequelize-cli模型自动生成