Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript “续集”;“模范员工”;财产不存在';你不再工作了?_Javascript_Mysql_Node.js_Sequelize.js_Sequelize Cli - Fatal编程技术网

Javascript “续集”;“模范员工”;财产不存在';你不再工作了?

Javascript “续集”;“模范员工”;财产不存在';你不再工作了?,javascript,mysql,node.js,sequelize.js,sequelize-cli,Javascript,Mysql,Node.js,Sequelize.js,Sequelize Cli,我正在对MySQL数据库使用sequelize v5.21和sequelize cli v5.5.1 我目前正尝试在以下模型下创建一些关联1:m,该模型从cli生成代码,如下所示: 其中:a为源,b为目标 model.associate = function (models) { // associations can be defined here a.hasOne(models.b); } 另一种模式,我有: model.associate = function (models) { /

我正在对MySQL数据库使用sequelize v5.21和sequelize cli v5.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()…”上得到一个错误,因为它不是一个函数

因此,我很好奇,在检查关联之前,我试着进行关联,如下所示:

a.hasOne(b);
b.belongsTo(a);
在其中工作很好

我的问题是,“model.associate”属性是否仍在v5^上工作


注意:我使用了sequelize文档中提供的检查关联方法,但我更喜欢这个方法,因为它更易于阅读。

我在v5的sequelize文档中没有找到关于
模型的任何信息。关联
,但在我的项目中,我在主模型目录中使用了以下代码:

Object.keys(db).forEach(modelName => {
    if (db[modelName].associate) {
        db[modelName].associate(db);
    }
});

其中,
db
由sequelize['import']

导入的sequelize cli模型自动生成是的,我在sequelize cli生成的“index.js”文件中有相同的代码,但问题仍然存在……太好了!当我把它放在index.js中时,它对我起了作用