【问题标题】:How to use insert() in sequelize migration?如何在续集迁移中使用 insert()?
【发布时间】:2017-11-09 06:09:30
【问题描述】:

我只想在 Sequelize 迁移中使用 insert() 创建一行。我已经看过bulkInsert() 的示例,但不想使用批量。 我们有这个函数只创建一行:

insert(instance, tableName, values, options)

但我不明白 param 中的实例是什么? 我正在使用bulkInsert,因为它不要求例如参数。 如果您可以在我下面编写的代码中添加插入,那就太好了。 迁移库:https://github.com/sequelize/sequelize/blob/master/lib/query-interface.js

//Code for insertion using bulkInsert
module.exports = {
  up: queryInterface => {
    queryInterface.describeTable("Platforms").then(attributes => {
      return queryInterface.bulkInsert("Platforms", [
        {
          id: 6,
          display_name: "Booking.com",
          code: "booking.com"
        }
      ]);
    });
  },
  down: queryInterface => {
    return queryInterface.bulkDelete("Platforms", {
      id: 6
    });
  }
};

【问题讨论】:

    标签: javascript node.js sequelize.js


    【解决方案1】:
      import Platform from '../models/Platform';
      module.exports = {
      up: queryInterface => {
        queryInterface.describeTable("Platforms").then(attributes => {
          return queryInterface.insert(Platform, "Platforms", [
            {
              id: 6,
              display_name: "Booking.com",
              code: "booking.com"
            }
          ]);
        });
      },
      down: queryInterface => {
        return queryInterface.bulkDelete("Platforms", {
          id: 6
        });
      }
    };
    

    这里的实例是您使用 sequilize 定义的模型实例。

    【讨论】:

      猜你喜欢
      • 2018-01-29
      • 2014-05-03
      • 1970-01-01
      • 2020-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多