我正在使用 node-orm2 和 Postgres 和 Express 在应用程序启动时创建表。我一直在筛选 GitHub 问题,但无法找出为什么没有创建其他列。我究竟做错了什么?
启动没有表的应用程序时的控制台输出:
[SQL/postgres] SELECT * FROM information_schema.tables WHERE table_name = 'user'
[SQL/postgres] CREATE TABLE "user" ("id" SERIAL, PRIMARY KEY ("id"))
model_user.js(应该创建 5 列,但只创建 1)
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();
}
}));
}