如何在 node.js 中使用 sequelize 在创建时将数据保存在数据库中
How to save data in database at the time of creation using sequelize in node.js
我正在尝试在创建数据库时保存批量数据。我的代码有以下问题:
- 如果我保持 models.sequelize.sync({force: true}).then(function(){ }); 那么它说 table 不是已创建。
- 如果我保留 {force:false} 那么在第一次部署中它会创建 table 并且在第二次部署中它会在数据库中保存数据。
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;
};
我无法弄清楚如何在数据库中部署应用程序时保存数据的正确方法
如何从模型定义中删除 bulkCreate
并将其放入 sync
回调中? (因为表将在sync
之后创建,所以我认为你不能在sync
完成之前bulkCreate
)
如下所示:
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"。 :)
我正在尝试在创建数据库时保存批量数据。我的代码有以下问题:
- 如果我保持 models.sequelize.sync({force: true}).then(function(){ }); 那么它说 table 不是已创建。
- 如果我保留 {force:false} 那么在第一次部署中它会创建 table 并且在第二次部署中它会在数据库中保存数据。
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;
};
我无法弄清楚如何在数据库中部署应用程序时保存数据的正确方法
如何从模型定义中删除 bulkCreate
并将其放入 sync
回调中? (因为表将在sync
之后创建,所以我认为你不能在sync
完成之前bulkCreate
)
如下所示:
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"。 :)