2016-02-11 31 views
1

我试图在创建数据库时保存大量的数据使用sequelize在创建时保存在数据库中的数据。 。我有我的代码如下问题:如何在node.js中

  • 如果我继续models.sequelize.sync({力:真正}),然后(函数(){});然后它说没有创建表。
  • 如果我继续{力:假}那么在第一次部署它创建表,并在第二部署其在数据库保存数据。

模型/ 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

正确的方法怎么样从模型中取出bulkCreate定义并将其放入sync回调? (因为表将sync后创建,所以我觉得你不能bulkCreatesync完成前)

象下面这样:

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应该写成英语“专业”的方式。 :)

+0

可以请你告诉我,我怎么能在每次部署时使用旧的数据库重新创建数据库。因为如果我保持{force:true},那么旧的数据库将被删除,并且新的被创建。 – Jennifer

+0

我不知道你的意思,我认为它属于一个新的问题:) – leroydev

+0

是啊,你是对的:)。我发布了一个新问题:http://stackoverflow.com/questions/35428355/how-to-recreate-database-from-old-database-using-sequelize-in-node-js – Jennifer