我正在尝试在创建数据库时保存批量数据。我的代码存在以下问题:
model / Speclization .js
module.exports = function(sequelize, DataTypes) {
var Speclization = sequelize.define("Speclization",
{
speclizationname: DataTypes.STRING
}, {
tableName: 'speclization', // this will define the table's name
timestamps: false // this will deactivate the timestamp columns
}
);
Speclization.bulkCreate([
{speclizationname: 'Computer Science'}, // part of records argument
{speclizationname: 'Information Technology'},
{speclizationname: 'Electrical'},
{speclizationname: 'Other'},
{speclizationname: 'Mechninncal'},
{speclizationname: 'Electronics'}
], ['speclizationname'])
return Speclization;
};
我无法弄清楚如何在数据库中的应用程序部署中保存数据的正确方法
答案 0 :(得分:0)
如何从模型定义中删除bulkCreate
并将其放入sync
回调中? (因为表格会在 sync
之后创建,所以我认为在bulkCreate
完成之前你不能sync
如下所示:
models.sequelize.sync({force: true}).then(function(){
Speclization.bulkCreate([
{speclizationname: 'Computer Science'}, // part of records argument
{speclizationname: 'Information Technology'},
{speclizationname: 'Electrical'},
{speclizationname: 'Other'},
{speclizationname: 'Mechninncal'},
{speclizationname: 'Electronics'}
]);
});
然后得到这样的模型定义:
module.exports = function(sequelize, DataTypes) {
var Speclization = sequelize.define("Speclization",
{
speclizationname: DataTypes.STRING
}, {
tableName: 'speclization', // this will define the table's name
timestamps: false // this will deactivate the timestamp columns
}
);
return Speclization;
};
顺便说一句,Speclization应该用英文写成“Specialization”。 :)