node.js - Sequelize 不同步 - 插入

标签 node.js sqlite express sequelize.js

这是我的代码:

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("/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:{

    }   
  });
};    

当我尝试使用下面的函数将某些内容保存到数据库时,它会在日志中显示 INSERT INTO 查询,但 db 文件中没有任何变化。

create_client:

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

我该如何解决这个问题?

最佳答案

我已经测试了您的应用程序,一切都插入正常,请查看https://github.com/finfort/SequelizeTestRepo

关于node.js - Sequelize 不同步 - 插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33973939/

相关文章:

node.js - Shopify App HMAC Validation Failed 如何解决?

c# - 使用 3 个文本框过滤数据库结果

node.js - Auth0 和 Express-openid-connect/回调没有响应

android - ContentUris.withAppendedId中 '_id'有什么用

javascript - 高速路由器

node.js - 更新用户配置文件时某些字段数据会被删除

javascript - 如何在客户端(AngularJS)加密密码,将其发送到服务器(expressJS)并在服务器上解密?

Node.js:短路由命名对 SEO 友好吗?

node.js - 如何检查请求是否在 Total.js Controller 中发布数据

sqlite - ScanID附近的语法错误