【问题标题】:Sequelize does not create hasMany associations but foreign keys defined in DBSequelize 不创建 hasMany 关联,但在 DB 中定义了外键
【发布时间】:2019-04-01 02:45:36
【问题描述】:

尽管当我在 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 检查数据库时,似乎关联定义正确。

【问题讨论】:

    标签: node.js mariadb sequelize.js


    【解决方案1】:

    好像是输入引起的问题。

    当你发送id属性时即使它的null也无所谓,sequelize不明白它是一个INSERT

    我正在发送如下记录;

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-24
      • 1970-01-01
      • 2018-01-05
      • 2021-04-21
      • 2019-09-03
      • 2017-08-29
      相关资源
      最近更新 更多