将Sequelize连接到与Homebrew一起安装的Postgres

时间:2012-07-09 03:38:48

标签: javascript postgresql node.js

我正在尝试使用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连接到数据库。我正在使用Sequelize的版本1.5.0-beta

编辑:

在我的入口点(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);

最后是models / post.js:

var Sequelize = require(“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, '-');
      }
    }
  });
};

1 个答案:

答案 0 :(得分:1)

首先,我建议使用sequelize.import。此外,我想知道你是否只是忘了sync数据库?如果您需要有关这些事项的更多详细信息,请与我们联系:)