dresende / node-orm2

Object Relational Mapping
http://github.com/dresende/node-orm2
MIT License
3.07k stars 379 forks source link

`db.sync` only creates the id column and nothing else #768

Closed dmr07 closed 7 years ago

dmr07 commented 7 years ago

db.sync() does not create all the columns. Please advise:

Console output when starting app with no tables.

[SQL/postgres] SELECT * FROM information_schema.tables WHERE table_name = 'user'
[SQL/postgres] CREATE TABLE "user" ("id" SERIAL, PRIMARY KEY ("id"))

model_user.js

var bcrypt = require('bcrypt-nodejs');

module.exports = function(db, cb) {
  return {
    id:          { type: 'serial', key: true},  // <------- only column that gets created
    name:  { type: 'text', size: 128 },
    email:       { type: 'text', size: 128 },
    createdAt  : { type: 'date', required: true, time: true },
    password:    { type: 'text', size: 128 },
  }, {
    hooks: {
      beforeValidation: function () {
        this.createdAt = new Date();
      }
    },
    methods : {
      generateHash: function(password) {
        return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
      },
      validPassword: function(password) {
        return bcrypt.compareSync(password, this.local.password);
      }
    } // end methods
  };
} // end exports

config_db.js

var orm = require('orm');
var model_user = require('../models/model_user');

module.exports = function(app) {
  var opts = {
    database : "somedb",
    protocol : "postgres",
    host     : "127.0.0.1",
    port     : 5432, 
    user     : "someguy",
    password : "",
    query    : {
      pool     : true,  
      debug    : true,  
      strdates : false
    }
  };

  app.use(orm.express(opts, {
    define: function (db, models, next) {
      models.user = db.define("user", model_user);
      db.sync();
      next();
    }
  }));
}
dmr07 commented 7 years ago

Just read the specification that Postgres is supported up to version 2.6.2. I'm using 9.6.2, could this be the cause?

dmr07 commented 7 years ago

Row insertion doesn't work either. I assume this is due to the model not having synced with the db.

I think I'll stick with postgres queries as unsightly as they are. Would like to come back to Node-ORM if you guys are able to point me to the right direction.

dxg commented 7 years ago
models.user = db.define("user", model_user);

model_user is a function in the above, not an object as expected. See https://github.com/dresende/node-orm2#introduction