简体   繁体   中英

Connecting Sequelize to Postgres installed with Homebrew

I'm trying to use Sequelize to connect to my local install of Postgres. I installed it via Homebrew and have confirmed that I can connect and query the database just fine. When I run Sequelize it outputs valid queries (I ran them via console) but the database doesn't change and doesn't log any connection. My current code is:

var sequelize = new Sequelize('reliable_rabbit', 'mohammad', null, {
  host: "127.0.0.1",
  dialect: 'postgres',
  define: { timestamps: true, paranoid: true, underscore: true }//,
});

I can connect to the database via: psql -d reliable_rabbit -U mohammad -h 127.0.0.1 . I am using version 1.5.0-beta of Sequelize.

Edit:

In my entry point (app/app.js), logs nothing:

var models = require('./models');
models.Post.sync().success(function(){
  console.log("Success!")
}).error(function(error){
  console.log("Error: " + error);
});

app/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);

and finally 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, '-');
      }
    }
  });
};

first of all i would recommend the use of sequelize.import . furthermore i'm wondering if you probably just forgot to sync the database? Please let me know if you need further details about those things :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM