0

I am working on updating my sequezlie config file so that it runs off env variables when the cli migration command is run. My setup works fine for remote servers that enable the env variables stored, but my local environment (like all) does not unless dotenv is run. I configured dotenv in my config file only if the environment recognized is development and my console log is outputting the correct process.env.DB_DIALECT of postgres, but for some reason, during the migration, sequelize is telling me to download mysql. Can anyone help with this bizarre issue?

Here is the head of my terminal output and error message:

DEVELOPMENT DOTENV
postgres
Loaded configuration file "config/config.js".
Error: Please install mysql package manually
    at new ConnectionManager (/Users/user/Desktop/Projects/node/app/node_modules/sequelize/lib/dialects/mysql/connection-manager.js:23:11)
    at new MysqlDialect (/Users/user/Desktop/Projects/node/app/node_modules/sequelize/lib/dialects/mysql/index.js:12:28)
    at new Sequelize (/Users/user/Desktop/Projects/node/app/node_modules/sequelize/lib/sequelize.js:220:18)

Here is my config file:

var express = require('express');
var app = express();
var path = require('path');

if(app.get('env') === 'development'){
    console.log('DEVELOPMENT DOTENV')
    require('dotenv').config();

};
console.log(process.env.DB_DIALECT) //postgres

module.exports = {
    "development": {
        "username": process.env.DB_LOCAL_USERNAME,
        "password": process.env.DB_LOCAL_PASSWORD,
        "database": process.env.DB_LOCAL_DATABASE,
        "host": "127.0.0.1",
        "dialect": process.env.DB_DIALECT,
        "migrationStorageTableName": "sequelize_meta",
        "autoMigrateOldSchema": true
    },
    "staging": {
        "username": process.env.RDS_USERNAME,
        "password": process.env.RDS_PASSWORD,
        "database": process.env.RDS_DATABASE,
        "host": process.env.RDS_HOSTNAME,
        "dialect": process.env.DB_DIALECT,
        "migrationStorageTableName": "sequelize_meta"
    },
    "production": {
        "username": process.env.RDS_USERNAME,
        "password": process.env.RDS_PASSWORD,
        "database": process.env.RDS_DATABASE,
        "host": process.env.RDS_HOSTNAME,
        "dialect": process.env.DB_DIALECT,
        "migrationStorageTableName": "sequelize_meta"
    }
}

Here is my migration file:

'use strict';

module.exports = {
  up: function (queryInterface, Sequelize) {
    queryInterface.sequelize.query("ALTER TABLE organization ADD CONSTRAINT organization_user_id_fkey FOREIGN KEY (admin_id) REFERENCES synotate_user (user_id) MATCH SIMPLE ON UPDATE CASCADE ON DELETE CASCADE;");
  },

  down: function (queryInterface, Sequelize) {
    queryInterface.sequelize.query("ALTER TABLE organization DROP CONSTRAINT organization_user_id_fkey;");
  }
};
cphill
  • 5,596
  • 16
  • 89
  • 182
  • My guess: Sequelize CLI does not know about your custom config path and is defaulting to mysql. Try creating a file `.sequelizerc` in your root with the following: `module.exports = {'config': './config/config.js'};` – mcranston18 Nov 16 '18 at 13:43
  • That unfortunately did not change the outcome. `var path = require('path'); module.exports = { 'models-path': path.resolve('./app/', 'models'), 'config': './config/config.js' }` outputs `Loaded configuration file "config/config.js". Error: Please install mysql package manually` – cphill Nov 18 '18 at 16:21
  • Hm. This is strange. I still suspect your env config is not being picked up properly by the Sequelize CLI. When you console.log `process.env.NODE_ENV`, what does it say? It must say `development`, `staging`, or `production` as those are the keys you have defined – mcranston18 Nov 18 '18 at 16:45

0 Answers0