1

I'm using Sequelize as an ORM and I'm trying to migrate my database to Heroku. When running heroku run sequelize db:migrate

I just get

Loaded configuration file "config/config.js".
Using environment "production".
ERROR: Error parsing url: undefined

Here's what my config file looks like:

  module.exports = {
  "development": {
    "username": "root",
    "password": "password",
    "database": "vueapp",
    "host": "127.0.0.1",
    "dialect": "postgres"
  },
  "test": {
    "username": "root",
    "password": null,
    "database": "database_test",
    "host": "127.0.0.1",
    "dialect": "mysql"
  },
  "production": {
    "use_env_variable": process.env.DATABASE_URL,
    "dialect": "postgres",
    "ssl": true,
    "dialectOptions": {
      "ssl": true
    }
  }
}

And the index file

const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const basename = path.basename(__filename);
const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../config/config')[env];

const User = require("./user")
const Hour = require('./hours');

const db = {
  User,
  Hour
};

let sequelize;
if (config.use_env_variable) {
  sequelize = new Sequelize(config.use_env_variable, config);
} else {
  sequelize = new Sequelize(config.database, config.username, config.password, config);
}

fs
  .readdirSync(__dirname)
  .filter(file => {
    return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
  })
  .forEach(file => {
    const model = sequelize['import'](path.join(__dirname, file));
    db[model.name] = model;
  });

Object.keys(db).forEach(modelName => {
  if (db[modelName].associate) {
    db[modelName].associate(db);
  }
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;

Not entirely sure what the issue is. Going through the heroku logs, I see a throw new Error('Dialect needs to be explicitly supplied as of v4.0.0');

But I have added the dialect in the config.js file. Does it matter if the config file is a .js file or a .json file?

Just to clarify, the actual web app opens up and I can see the error handler that I set up. So I'm not actually getting an application error when opening up the route URL

1
  • Did you later solve the problem. I need help Commented Apr 29, 2021 at 5:11

3 Answers 3

1

production: { use_env_variable: 'DATABASE_URL', },

change your production to this and go to heroku to set environmental variable

Sign up to request clarification or add additional context in comments.

Comments

0

Did you later solve the problem

Comments

0

This is a little confusing but Damilola is correctly. You need need to leave the value for use_env_variable to 'DATABASE_URL'. You will be tempted to change this to process.env.DATABASE_URL. This is incorrect.

Leaving it like this will allow Sequelize to inject the value that is saved to the Heroku env variable DATABASE_URL.

production: { use_env_variable: 'DATABASE_URL', dialect: "postgres", protocol: "postgres" }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.