wuno
wuno

Reputation: 9875

Sequelize migrate failing due to dialect object Object not supported error

Background

I am creating a boilerplate express application. I have configured a database connection using pg and sequelize. When I add the cli and try to run sequlize db:migrate I get this error,

ERROR: The dialect [object Object] is not supported. Supported dialects: mssql, mysql, postgres, and sqlite.

Replicate

Generate a new express application. Install pg, pg-hstore, sequelize and sequelize-cli.

Run sequelize init.

Add a config.js file to the /config path that was created from sequelize init.

Create the connection in the config.js file.

Update the config.json file created by sequelize-cli.

Run sequelize db:migrate

Example

/config/config.js

const Sequelize = require('sequelize');
const { username, host, database, password, port } = require('../secrets/db');

const sequelize = new Sequelize(database, username, password, {
  host,
  port,
  dialect: 'postgres',
  operatorsAliases: false,
  pool: {
    max: 5,
    min: 0,
    acquire: 30000,
    idle: 10000
  }
});

module.exports = sequelize;

/config/config.js

{
  "development": {
    "username": "user",
    "password": "pass",
    "database": "db",
    "host": "host",
    "dialect": "postgres"
  },
  "test": {
    "username": "user",
    "password": "pass",
    "database": "db",
    "host": "host",
    "dialect": "postgres"
  },
  "production": {
    "username": "user",
    "password": "pass",
    "database": "db",
    "host": "host",
    "dialect": "postgres"
  }
}

Problem

I expect the initial migrations to run but instead get an error,

ERROR: The dialect [object Object] is not supported. Supported dialects: mssql, mysql, postgres, and sqlite.

Versions

Dialect: postgres 
Dialect version: "pg":7.4.3 
Sequelize version: 4.38.0
Sequelize-Cli version: 4.0.0

Package Json

"pg": "^7.4.3",
"pg-hstore": "^2.3.2",
"sequelize": "^4.38.0"

Installed globally

npm install -g sequelize-cli

Question

Now that the major rewrite has been released for sequelize, what is the proper way to add the dialect so the migrations will run?

It is important to note that my connection is working fine. I can query the database without problems, only sequelize-cli will not work when running migrations.

Upvotes: 0

Views: 7688

Answers (2)

samueldario
samueldario

Reputation: 11

Hi Wuno and Hi everyone,

I've had the same problem recently and this was very annoying.

The problem: ERROR: The dialect [object Object] is not supported. Supported dialects: mssql, mysql, postgres, and sqlite.

First we need to know that: sequelize needs one type of connection and our program needs another.

For sequelize I have .sequelizerc:

const path = require('path');

module.exports = {
    config: path.resolve(__dirname, 'src', 'config', 'database.js'),
    'migrations-path': path.resolve(__dirname, 'src', 'database', 'migrations'),
    'seeders-path': path.resolve(__dirname, 'src', 'database', 'seeders'),
    'models-path': path.resolve(__dirname, 'src', 'api', 'models')
}

And the connection in my database.js:

const dotenv = require('dotenv');
dotenv.config();

module.exports = {
    dialect: process.env.DB_DIALECT,
    host: process.env.DB_HOST,
    username: process.env.DB_USER,
    password: process.env.DB_PWD,
    database: process.env.DB_NAME,
    define: {
        timestamps: true,
        underscored: true
    }
}

For sequelize is this, now you can run your db:migrate without problem, but if you try to use your app will start to message that Error...

After this you will have other errors, problably in your models when you try to run your code (npm start or nodemon...). So, now you'll need to solve this with another connection, like I said before.

The new connection will I gave the name database_app.js:

const dotenv = require('dotenv');
dotenv.config();
const { Sequelize } = require('sequelize');

const sequelize = new Sequelize(
    process.env.DB_NAME,
    process.env.DB_USER,
    process.env.DB_PWD,
    {
        host: process.env.DB_HOST,
        dialect: process.env.DB_DIALECT,
        logging: false
    }
);
//console.log(sequelize);


module.exports = sequelize;

After this you need to change the configuration of your code, remember for sequelize is one and for run the code is another.

Regards.

Upvotes: 1

m_____ilk
m_____ilk

Reputation: 173

i ran into same problem. there is a few thing that you need to change. first, i am not sure why you had 2 config/config.js file. i assumed the second file is config.json. the reason run into this problem is that

const sequelize = new Sequelize(database, username, password, {
  host,
  port,
  dialect: 'postgres',
  operatorsAliases: false,
  pool: {
    max: 5,
    min: 0,
    acquire: 30000,
    idle: 10000
  }
});

these lines of code is used for the node server to access db, not for sequlize-cli to migrate. you need to follow exact the sequlize-cli instruction. here is the link: instruction

my code: config/db.js

const {sequlize_cli} = require('../config.json');

module.exports = sequlize_cli;

config.json

{
    "sequlize_cli":{
        "development":{
            "username":"root",
            "password":"passowrd",
            "database":"monitor",
            "host":"127.0.0.1",
            "dialect": "postgres"
        },
        "test": {
            "username":"root",
            "password":"passowrd",
            "database":"monitor",
            "host":"127.0.0.1",
            "dialect": "postgres"
          },
          "production": {
            "username":"root",
            "password":"passowrd",
            "database":"monitor",
            "host":"127.0.0.1",
            "dialect": "postgres"
          }
    }
}

the main point i guess is to export the json object directly instead of exporting a sequelize object. In addition, this is only the problem with postges , i tested with mysql, your code works perfectly with mysql.

Upvotes: 0

Related Questions