Sequelize Migration Error: Dialect needs to be explicitly supplied as of v4.0.0

Viewed 381

This is my config.js file

require('dotenv').config();

module.exports = {
  test: {
    username: process.env.DB_USER_2,
    password: process.env.DB_PASS_2,
    database: process.env.DB_TABLE,
    host: process.env.DB_HOST_2,
    dialect: "mysql"
  },
  production: {
    username: process.env.DB_USER_PRODUCTION,
    password: process.env.DB_PASS_PRODUCTION,
    database: process.env.DB_TABLE_PRODUCTION,
    host: process.env.DB_HOST_PRODUCTION,
    dialect: "mysql"
  }
}

This is my index.js file within my models folder

'use strict';

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.js')[env];
const db = {};

let sequelize;
if (config.use_env_variable) {
  console.log(process.env)
  sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
  sequelize = new Sequelize(config.database, config.username, config.password, config);
}

I have a .env file with the current environment of testing, I'm trying to run a migration right now, Right now I emptied the migration file incase anything there was messing up the sequelize. When I run db:migrate:status it throws the error about the database dialect. Does anyone know why the dialect error gets thrown?

1 Answers

The issue i faced with this was that the index file was set for development, yet there was no development option in my config.js, the issue went away once I updated that.

Update your example to look like this:

require('dotenv').config();

module.exports = {
  development: {
    username: process.env.DB_USER_2,
    password: process.env.DB_PASS_2,
    database: process.env.DB_TABLE,
    host: process.env.DB_HOST_2,
    dialect: "mysql"
  },
  test: {
    username: process.env.DB_USER_2,
    password: process.env.DB_PASS_2,
    database: process.env.DB_TABLE,
    host: process.env.DB_HOST_2,
    dialect: "mysql"
  },
  production: {
    username: process.env.DB_USER_PRODUCTION,
    password: process.env.DB_PASS_PRODUCTION,
    database: process.env.DB_TABLE_PRODUCTION,
    host: process.env.DB_HOST_PRODUCTION,
    dialect: "mysql"
  }
}

update your config file and you should be good to go.

Related