我正在尝试使用Sequelize连接到我本地安装的 Postgres。我通过Homebrew安装了它,并确认我可以很好地连接和查询数据库。当我运行 Sequelize 时,它会输出有效查询(我通过控制台运行它们),但数据库不会更改,也不会记录任何连接。我目前的代码是:
var sequelize = new Sequelize('reliable_rabbit', 'mohammad', null, {
host: "127.0.0.1",
dialect: 'postgres',
define: { timestamps: true, paranoid: true, underscore: true }//,
});
我可以通过以下方式连接到数据库:psql -d reliable_rabbit -U mohammad -h 127.0.0.1
. 我正在使用1.5.0-beta
Sequelize 版本。
编辑:
在我的入口点 (app/app.js) 中,什么也不记录:
var models = require('./models');
models.Post.sync().success(function(){
console.log("Success!")
}).error(function(error){
console.log("Error: " + error);
});
应用程序/models.js:
var Sequelize = require("sequelize")
var sequelize = new Sequelize('reliable_rabbit', 'mohammad', null, {
host: "127.0.0.1",
logging: console.log,
dialect: 'postgres',
define: { timestamps: true, paranoid: true, underscore: true }//,
});
exports.Post = require("../models/post")(sequelize);
最后是模型/post.js:
var Sequelize = 要求(“续集”);
module.exports = function(sequelize) {
return sequelize.define('post', {
title: { type: Sequelize.STRING, validate: { notEmpty: true } },
permalink: { type: Sequelize.STRING, validate: { is: ["^[a-z]+[a-z\-][a-z]+$"] } },
content: { type: Sequelize.TEXT, validate: { notEmpty: true } },
published: { type: Sequelize.BOOLEAN, defaultValue: false },
published_at: { type: Sequelize.DATE }
},{
instanceMethods: {
generatePermalink: function() {
this.permalink = this.title.toLowerCase().replace(/[^a-z]/g, "-").replace(/^-+|-+$/g,'').replace(/\-{2,}/g, '-');
}
}
});
};