Sequelize不会创建hasMany关联,但是会在DB中定义外键

时间:2018-10-27 16:25:45

标签: node.js mariadb sequelize.js

虽然我在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检查数据库时,似乎正确定义了关联。 enter image description here

1 个答案:

答案 0 :(得分:0)

似乎是输入引起的问题。

当您发送id属性甚至与它的null无关时,续集也不知道它是INSERT

我正在发送如下记录;

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