Search code examples
node.jspostgresqlherokusequelize.jsheroku-postgres

Sequelize migration in Heroku Postgres: Chain of errors


My express.js app is on a heroku dyno. I created some rest api endpoints for crud operations (connected to heroku-postgres db) and checked they worked with Postman.

My problems have started after I tried incorporating migrations. I am just a jr. dev so please let me know if I am investigating dead leads, and what other information to provide to properly diagnose these error messages.

Starting migrations with sequelize model:create --name tableName --attributes ... was fine.

When running sequelize db:migrate, the error I get is:

ERROR: Dialect needs to be explicitly supplied as of v4.0.0

My current config.json is

{
  "production": {
    "use_env_variable": "DATABASE_URL",
    "dialect": "postgres",
    "dialectOptions": {
      "ssl": {
        "require": true
      }
    },
    "ssl": true
  }
}

and I have also specified the dialect in models/index.js (this was generated along with config.json in the sequelize model:create... command)

require('dotenv').config();
const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const basename = path.basename(__filename);
const env = 'production' || process.env.NODE_ENV;
const config = require(__dirname + '/../config/config.json')[env];
const db = {};

const sequelize = new Sequelize(process.env.DATABASE_URL, {
  logging: false,
  dialect: "postgres",
  dialectOptions: {
    ssl: true,
  }
});
// let sequelize;
// if (config.use_env_variable) {
//   sequelize = new Sequelize(process.env[config.use_env_variable], config);
// } else {
//   sequelize = new Sequelize(config.database, config.username, config.password, config);
// }

If const sequelize = ... is commented out, and let sequelize; ... is used instead, the same dialect error occurs.

Interestingly, when I set the shell variable export NODE_ENV=production, a new error occurs - ERROR: Error parsing url: undefined.

This is more baffling to me as I thought it was already defined in the const env = ... line in index.js.

So then I tried specifying url in the migration command - sequelize db:migrate --url 'postgres://username:password@localhost/test1'.

This url that I have provided worked for the CRUD testing via Postman, as well as via the front-end collecting the form data.

Now the error is ERROR: connect ECONNREFUSED 127.0.0.1:5432. I am unsure how to proceed from this point, and not sure that the steps I have taken since the dialect error are the correct ways to fix these problems.

My questions really are the cause of these errors.

1) Where else do I need to specify dialect?

2) Why does the dialect error go away when I export NODE_ENV, even though the environment is already specified in index.js?

3) Similarly for the url, why won't url from my .env brought in with require('dotenv').config() be accepted?


Solution

  • It was all well and good to generate migration and model files using the sequelize-cli. I had pushed these changes to heroku but was running the migration commands locally. (I do not have a local dev database, my only environment is production).

    Another problem was, the heroku bash ran in what I can only describe as a snapshot of the application's state when heroku run bash is run. This meant that even though I was creating, editing, and pushing just fine, the migration commands would be 'stuck' on the old version until the bash was restarted.