簡體   English   中英

Sequelize不會創建hasMany關聯,但是會在DB中定義外鍵

[英]Sequelize does not create hasMany associations but foreign keys defined in DB

當我在manufacturer模型上調用create時,似乎我在數據庫中定義的關聯似乎沒有創建。

這些是我關聯的模型。

ManufacturerText.associate = function (models) {
    models.manufacturer_text.belongsTo(models.language, {
      as: 'language'
    });
    models.manufacturer_text.belongsTo(models.manufacturer, {
      as: 'manufacturer'
    });
  };

  ManufacturerVideo.associate = function (models) {
    models.manufacturer_video.belongsTo(models.language, {
      as: 'language'
    });
    models.manufacturer_video.belongsTo(models.video_type, {
      as: 'video_type'
    });
    models.manufacturer_video.belongsTo(models.manufacturer, {
      as: 'manufacturer'
    });
  }

這是主要模型:

  Manufacturer.associate = function(models) {

// models
    models.manufacturer.hasMany(models.manufacturer_text, {foreignKey:'manufacturer_id', as: 'manufacturer_translations' });
    models.manufacturer.hasMany(models.manufacturer_video, {foreignKey:'manufacturer_id', as: 'manufacturer_videos' });
    models.manufacturer.hasMany(models.inspiration_image, {foreignKey:'manufacturer_id', as: 'inspirations' });

    models.manufacturer.belongsTo(models.file, {as: 'image'});
    models.manufacturer.belongsTo(models.file, {as: 'header_image'});

  };

以上兩個協會都不起作用。

當我使用MySQL Workbench檢查數據庫時,似乎關聯定義正確。 在此處輸入圖片說明

看來問題是由輸入引起的。

當您發送id屬性時,即使它為null也不重要,sequelize不理解它是INSERT

我正在發送如下記錄:

{
  delivery_days: 10,
  image_id: 4,
  { id: null, url: 'http://url', ... }
}

暫無
暫無

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

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