2015-11-28 27 views
1

這裏是我的代碼:Sequelize不同步 - 插入

models/index.js

var Sequelize = require('sequelize'); 
// initialize database connection 
var sequelize = new Sequelize('somedb', '', '', { 
    host: 'localhost', 
    dialect: 'sqlite', 
    pool:{ 
    max: 5, 
    min: 0, 
    idle: 10000 
    }, 
    storage: "db/main.db" 
}); 

// load models 
var models = [ 
    'Users',"Clients" 
]; 
models.forEach(function(model) { 
    module.exports[model] = sequelize.import(__dirname + '/' + model); 
    module.exports[model].sync(); 
}); 

// export connection 
module.exports.sequelize = sequelize; 

app.js

SQLized.sync().then(function(err){ 
    //Settings Environmental Variables 
    var env = process.env.STAGE; 
    var config = require("./config.js")[env]; 
    ... 
}); 

models/Users.js

var Sequelize = require("sequelize"); 
var hash = require("../modules/util/hash"); 

module.exports=function(sequelize, DataTypes){ 
    return Users = sequelize.define("Users", { 
    id: { 
     type: DataTypes.INTEGER, 
     field: "id", 
     autoIncrement: !0, 
     primaryKey: !0 
    }, 
    firstName: { 
     type: DataTypes.STRING, 
     field: "first_name" 
    }, 
    lastName: { 
     type: DataTypes.STRING, 
     field: "last_name" 
    }, 
    username: { 
     type: DataTypes.STRING, 
     field: "username" 
    }, 
    email: { 
     type: DataTypes.STRING, 
     field: "email" 
    }, 
    password: { 
     type: DataTypes.STRING, 
     field: "password"   
    }, 
    token: { 
     type: DataTypes.STRING, 
     field: "access_token" 
    }, 
    isAdmin: { 
     type: DataTypes.BOOLEAN, 
     field: "isAdmin"  
    } 
    }, { 
    freezeTableName: true, // Model tableName will be the same as the model name 
    classMethods:{ 
     usernameExists: function(username, _clb){ 

     }, 
     userExists: function(username, _clb){ 

     }, 
     signup: function(params, _clb){ 
     var fullnm_splt = params.fullname.split(' '); 
     params.firstName = (fullnm_splt.length >2) ? fullnm_splt.slice(0, -1).join(" ") : fullnm_splt[0]; 
     params.lastName = (fullnm_splt.length >2) ? fullnm_splt.slice(-1).join(" ") : fullnm_splt[1]; 

     res.redirect("https://stackoverflow.com/users/" + params.username); 


     } 
    }, 
    instanceMethods:{ 
     isValidPassword: function(password, _clb){ 
     var _self = this; 
     hash(password, function(err, password_encr){ 
      if(err){ 
      return _clb(err); 
      } 
      if(password_encr == _self.password){ 
      return _clb(null, true); 
      } 

      return _clb(null, false); 
     }); 

     } 
    } 
    }); 
}; 

models/Clients.js

var Sequelize = require("sequelize"); 

module.exports=function(sequelize, DataTypes){ 
    return Clients = sequelize.define("Clients", { 
    id:{ 
     autoIncrement: true, 
     primaryKey: true, 
     type: DataTypes.INTEGER 
    }, 
    client_name: { 
     type: DataTypes.STRING, 
     field: "client_name" 
    }, 
    client_host_name: { 
     type: DataTypes.STRING, 
     field: "client_host_name" 
    }, 
    ip: { 
     type: DataTypes.STRING, 
     field: "ip" 
    }, 
    mac: { 
     type: DataTypes.STRING, 
     field: "mac" 
    }, 
    previous_ip: { 
     type: DataTypes.STRING, 
     field: "previous_ip" 
    } 
    }, {  
    freezeTableName: true, // Model tableName will be the same as the model name 
    classMethods:{ 

    },  
    instanceMethods:{ 

    } 
    }); 
};  

當我嘗試保存的東西DB,與下面的函數,它指出在日誌中INSERT INTO查詢,但沒有在數據庫文件的變化。

create_client

Clients 
      .create({client_host_name: params.client_host_name, ip: params.ip, mac: params.mac}) 
      .then(function(err){ 
      res.send("OK"); 
      }); 

我該如何解決這個問題?

回答

-1

我已測試過您的應用程序,所有內容都可以插入檢查出https://github.com/finfort/SequelizeTestRepo