我想在 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/