对于MySQL数据库,我使用SequelizeV5.21和sequelize -CLIV5.5.1。
我目前正在尝试在以下模型下创建一些关联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()...“as不是一个函数...
所以,我有足够的好奇心,在检查关联之前尝试做这些关联:
a.hasOne(b);
b.belongsTo(a);在其中工作正常...
我的问题是,"model.associate“属性是否仍然适用于v5^?
注意:我使用了sequelize文档中提供的check associations方法,但我更喜欢这个方法,因为它更容易阅读。
发布于 2019-12-23 19:41:34
我在v5的Sequelize文档中没有找到关于model.associate的任何东西,但是在我的项目中,我在主models目录中使用了以下代码:
Object.keys(db).forEach(modelName => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});其中,由sequelize['import']导入的sequelize-cli模型自动生成db
https://stackoverflow.com/questions/59378291
复制相似问题