且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Sequelize属于不向数据库表添加外键约束

更新时间:2023-01-29 08:41:55

通过在 sequelize 的单例模块中编写几行代码,我自己解决了问题:

Resolved the problem at my own by writing the few lines of code in my singleton module of sequelize:

Object.keys(db).forEach(function(modelName) {
  if(db[modelName].hasOwnProperty('associate')) {
    db[modelName].associate(db);
  }
});

代码将遍历所有模型并直接调用它们的关联方法,因为它们已被定义为 classMethods.

The code will iterate over all the models and call their associate methods directly as they have been defined as classMethods.