node.js - Sequelize : One-to-Many relationship not working when inserting values

标签 node.js orm sequelize.js one-to-many sequelize-cli

我在两个对象之间建立了一对多关系:仪表板和图表,描述如下:

module.exports = function (sequelize, DataTypes) {
  const Dashboard = sequelize.define('Dashboard', {
      id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    title: {
      type: DataTypes.STRING
    }
  });

  Dashboard.associate = (models) => {
    Dashboard.belongsTo(models.User, {
      foreignKey: 'user_id',
      targetKey: 'id'
    });

    Dashboard.hasMany(models.Chart, {
      foreignKey: 'dashboard_id',
      sourceKey: 'id'
    });
  };

  return Dashboard;
};

和:

module.exports = function(sequelize, DataTypes) {
  var Chart = sequelize.define('Chart', {
      id: {
        type: DataTypes.INTEGER(32),
        primaryKey: true,
        autoIncrement: true
      },
      title: {
        type: DataTypes.STRING,
        allowNull: false
      },
      x_title: {
        type: DataTypes.STRING
      },
      y_title: {
        type: DataTypes.STRING
      },
      data_type: {
        type: DataTypes.STRING,
        allowNull: false
      },
      data_value: {
        type: DataTypes.STRING,
        allowNull: false
      },
      filters: {
        type: DataTypes.TEXT,
        allowNull: false
      }
    }
  );

  Chart.associate = (models) => {
    Chart.belongsTo(models.Dashboard, {
      foreignKey: 'dashboard_id',
      targetKey: 'id'
    });
  };

  return Chart;
};

所以当我想添加一个包含多个图表的新仪表板时:

models.Dashboard.create({
    user_id: 1,
    title: 'Dashboard title',
    charts: [
      {
        title: 'time',
        x_title: 'Days',
        y_title: 'Count',
        data_type: 'count',
        data_value: 'visit',
        filters: '[{"type":"project","values":["1"]},{"type":"language","values":["french"]},{"type":"satisfaction","values":[1,2,3]}]'
      },
      {
        title: 'indicator',
        x_title: '',
        y_title: '',
        data_type: 'count',
        data_value: 'visit',
        filters: '[{"type":"project","values":["1","2","3","4","5","6"]},{"type":"language","values":["french"]}]'
      }
    ]
  }, { include: [models.Chart] }).then(() => {
    res.send({
      'message': 'Dashboard loaded !'
    });
  });

仪表板已插入数据库,但也没有插入图表...使用 Sequelize 添加具有一对多关联的记录的最佳方法是什么?我只是尝试并阅读所有文档(http://docs.sequelizejs.com/manual/tutorial/associations.html#creating-with-associations),但我不明白这种有问题的行为......

感谢您的帮助和启发!

最佳答案

在定义的关联中

Dashboard.hasMany(models.Chart, {
      foreignKey: 'dashboard_id',
      sourceKey: 'id'
    });

foreignKey 被称为 dashboard_id 但在 Chart 模型中没有 dashboard_id

下面的代码有效

const Dashboard = sequelize.define('dashboard', {
  title: Sequelize.STRING,
});

const Chart = sequelize.define('chart', {
  dashboard_id: Sequelize.INTEGER,
  title: Sequelize.STRING,
});

Dashboard.hasMany(Chart, {
  foreignKey: 'dashboard_id',
  sourceKey: 'id'
});
Dashboard.create({
  title: 'one',
  charts: [
    { title: 'title1' },
    { title: 'title2' }
  ]
}, { include: [Chart] }).then((result) => {
  console.log(result);
});

关于node.js - Sequelize : One-to-Many relationship not working when inserting values,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50685266/

相关文章:

sql - MEAN-Stack - 无法从服务器读取第二个 SQL 表

node.js - Now.js 中的 session 支持

node.js - Mongodb数据库连接代码破坏了应用程序

php - Laravel ORM 问题

sql - 设计题: Filterable attributes, SQL

sqlite - 关联子句外键不匹配错误

node.js - 使用 should.js 履行 promise 后执行自定义测试的语法是什么?

javascript - $ (Node+Express+Pug+JQuery) 上未定义

postgresql - 直接 hibernate native 查询工作正常,但使用 setParameter 查询未更新 postgresql 数据库

javascript - 如何以编程方式自动化 sequelize 请求语句?