【问题标题】:how to create or update multiple tables using Sequelize?如何使用 Sequelize 创建或更新多个表?
【发布时间】:2020-02-19 15:04:39
【问题描述】:

我正在使用 Sequelize ORM 开发带有 DB 的后端。我需要将数据创建到多个表中或将数据更新到多个表中。但我不知道该怎么做。以下是我的示例:

//Tables
const main = sequelize.define('main', {
id: {type: Sequelize.STRING, primaryKey: true},
column1: Sequelize.STRING,
column2: Sequelize.STRING,
column3: Sequelize.STRING,
}, {
tableName: 'main'
});

const table1 = sequelize.define('table1', {
column1: Sequelize.STRING,
column2: Sequelize.STRING,
column3: Sequelize.STRING,
mainId: Sequelize.STRING,
}, {
tableName: 'table1'
});

const table2 = sequelize.define('table1', {
column1: Sequelize.STRING,
column2: Sequelize.STRING,
column3: Sequelize.STRING,
mainId: Sequelize.STRING,
}, {
tableName: 'table2'
});

//relation with tables
table1.belongsTo(MAIN, {foreignKey: 'mainId'});
MAIN.hasMany(table1, {foreignKey: 'mainId'});
table2.belongsTo(MAIN, {foreignKey: 'mainId'});
MAIN.hasMany(table2, {foreignKey: 'mainId'});
table3.belongsTo(MAIN, {foreignKey: 'mainId'});
MAIN.hasMany(table3, {foreignKey: 'mainId'});

//Input Body(JSON)
{
"id": "MAIN0001",
"column1": "string",
"column1": "string",
"column3": "string",
"table1": {
  "column1": "string",
  "column2": "string",
  "column3": "string",
  "mainId": "MAIN0001",
  },
"table1": {
  "column1": "string",
  "column2": "string",
  "column3": "string",
  "mainId": "MAIN0001",
  },
"table2": {
  "column1": "string",
  "column2": "string",
  "column3": "string"
  "mainId": "MAIN0001",
  }
}

这就是我要做的所有例子。 请让我知道我该怎么做。

提前致谢:D

【问题讨论】:

    标签: javascript mysql sql express sequelize.js


    【解决方案1】:

    有很多方法。这里有几个

    使用交易

    try {
    
      const result = await sequelize.transaction(async (t) => {
    
        const createdMain = await main1.create({
          column1: 'string',
          column2: 'string',
          column3: 'string',
        }, { transaction: t });
    
        const createdTable1 = await table1.create({
          column1: 'string',
          column2: 'string',
          column3: 'string',
          mainId: createdMain.id
        }, { transaction: t });
    
        return {
          ...createdMain,
         table1: createdTable1,
        };
      });
    
    } catch (error) {
    
      // If the execution reaches this line, an error occurred.
      // The transaction has already been rolled back automatically by Sequelize!
    
    }
    

    一步创建 main 和 table1:

    return main1.create({
       column1: 'string',
       column2: 'string',
       column3: 'string',
       table1: {
         column1: 'string',
         column2: 'string',
         column3: 'string',
       },
       // Repeat for table 2 and table 3
    }, {
        include: [ table1 ]
      // Include table 2 and table 3
      }]
    });
    

    【讨论】:

    • 哦,谢谢回答 :) 顺便说一句,我很好奇。是否也可以在具有 1:N 关系的表中创建数据行?
    • 喜欢从table1 @Christopher 创建main1 数据?
    猜你喜欢
    • 2013-08-20
    • 1970-01-01
    • 2018-09-10
    • 2018-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-09
    • 1970-01-01
    相关资源
    最近更新 更多