javascript - 如何在 sequelize 迁移中使用 insert()?

标签 javascript node.js sequelize.js

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

insert(instance, tableName, values, options)

但是我不明白param里的instance是什么? 我正在使用 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
    });
  }
};

最佳答案

  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 定义的模型实例。

关于javascript - 如何在 sequelize 迁移中使用 insert()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44420267/

相关文章:

javascript - 将图像挤压到窗口中,除非窗口比图像大

javascript - 如何防止我的产品列表在父组件购物车状态更新时重新呈现?

javascript - 将样式转换回 CSS 默认值

javascript - 使用 JavaScript 时,如果某个值是 "not defined",这意味着什么?

javascript - ES6 导入、导出 = 错误 : invalid argument

node.js - 使用 Sequelize 拉取关联值

node.js - 如何使用sequelize隐藏链接查询中的关联表结果?

javascript - 如何将 Nodejs 与 R 结合使用来获取结果

mysql - Node.js 在使用变量之前尝试使用 async/await 从 mysql select 获取数据

node.js - 我使用 Sequelize 的数据库模型不进行迁移