簡體   English   中英

續集“model.associate”屬性不再起作用?

[英]Sequelize "model.associate" property doesn't work anymore?

我正在將 sequelize v5.21 和 sequelize-cli v5.5.1 用於 MySQL 數據庫。

我目前正在嘗試在從 cli 生成的以下模型代碼下創建一些關聯 1:M,如下所示:

其中: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 文檔中沒有發現有關model.associate任何信息,但在我的項目中,我在主模型目錄中使用了此代碼:

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

其中dbsequelize['import'] sequelize-cli 模型自動生成

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM