9

I've come across several posts for this question however, none of them seem to have an actual answer. Several ideas, yet none of them work.

After digging around both the Sequelize and Tedious packages and watching my config get passed down correctly, I'm at a loss.

I am trying to run migrations against a new database in MSSQL. I have no problem connecting to it with the same creds I'm using here so I know that's not the issue.

I have my config.js that is pulling env vars. With the exception of my custom console statements, this file was auto generated from sequelize and is correctly referenced in my sequelizerc

require('dotenv').config()
console.log('[+] Loading database config...')

if (process.env.NODE_ENV === 'production') {
  console.log(`[+] Using database: ${process.env.PROD_DB_DATABASE}`)
} else if (process.env.NODE_ENV === 'development') {
  console.log(`[+] Using database: ${process.env.DEV_DB_DATABASE}`)
} else if (process.env.NODE_ENV === 'test') {
  console.log(`[+] Using database: ${process.env.TEST_DB_DATABASE}`)
} else if (process.env.NODE_ENV === 'local') {
  console.log(`[+] Using database: ${process.env.LOCAL_DB_DATABASE}`)
} else {
  console.log(`[-] CANNOT LOAD DATABASE FROM ENV: ${process.env.NODE_ENV}`)
  process.exit()
}

module.exports = {
  production: {
    database: process.env.PROD_DB_DATABASE,
    username: process.env.PROD_DB_USERNAME,
    password: process.env.PROD_DB_PASSWORD,
    host: process.env.PROD_DB_HOST,
    port: process.env.PROD_DB_PORT,
    dialect: process.env.PROD_DB_DIALECT,
    storage: process.env.PROD_DB_STORAGE,
    logging: false,
    dialectOptions: {
      instanceName: process.env.PROD_INSTANCE_NAME
    },
    pool: {
      min: 5,
      max: 1,
      acquire: 6000,
      idle: 6000
    }
  },
  development: {
    database: process.env.DEV_DB_DATABASE,
    username: process.env.DEV_DB_USERNAME,
    password: process.env.DEV_DB_PASSWORD,
    host: process.env.DEV_DB_HOST,
    port: process.env.DEV_DB_PORT,
    dialect: process.env.DEV_DB_DIALECT,
    storage: process.env.DEV_DB_STORAGE,
    logging: console.log,
    dialectOptions: {
      instanceName: process.env.DEV_INSTANCE_NAME,
      debug: true
    },
    pool: {
      min: 5,
      max: 1,
      acquire: 6000,
      idle: 6000
    }
  },
  test: {
    database: process.env.TEST_DB_DATABASE,
    username: process.env.TEST_DB_USERNAME,
    password: process.env.TEST_DB_PASSWORD,
    host: process.env.TEST_DB_HOST,
    port: process.env.TEST_DB_PORT,
    dialect: process.env.TEST_DB_DIALECT,
    storage: process.env.TEST_DB_STORAGE,
    logging: false
  },
  local: {
    database: process.env.LOCAL_DB_DATABASE,
    username: process.env.LOCAL_DB_USERNAME,
    password: process.env.LOCAL_DB_PASSWORD,
    host: process.env.LOCAL_DB_HOST,
    port: process.env.LOCAL_DB_PORT,
    dialect: process.env.LOCAL_DB_DIALECT,
    storage: process.env.LOCAL_DB_STORAGE,
    logging: false
  }
}

When i run my migration i get the error:

> node_modules/.bin/sequelize db:migrate

// ERROR: Login failed for user ''.

As mentioned above I dug through sequelize and tedious and my config is getting passed properly through both so i know it's not an env var issue or a NODE_ENV issue.

Anyone have any ideas here? I'm about to smash my face into my keyboard.

2 Answers 2

14

More for older versions:
If you are using sequelize@4, then it seems there is a hidden requirement that you must use tedious@<=5.


Which version of Sequelize are you using? If it's v5, According to Sequelize v5's document:

Sequelize now works with tedious >= 6.0.0

However, in its package.json, it does not depend on tedious at all. Since your program still runs, I guess you manually installed an older version of tedious before, which caused this strange problem.

Manually installing tedious of version>=6 should solve this problem, just like stated in its Getting started document page:

You'll also have to manually install the driver for your database of choice:
# One of the following: $ npm install --save pg pg-hstore # Postgres $ npm install --save mysql2 $ npm install --save mariadb $ npm install --save sqlite3 $ npm install --save tedious # Microsoft SQL Server

3
  • I will give this a shot! Currently putting out a fire in another code base but when i get back to this i'll give it a go.
    – rimraf
    Commented May 30, 2019 at 15:49
  • Works for me. I had this error with sequelize 5.22.3 and tedious 3.0.1. Upgraded to tedious 9.2.1 and it fixed the MSSQL connection. Commented Sep 7, 2020 at 19:23
  • If you are using sequelize@4, then it seems there is a hidden requirement that you must use tedious@<=5. This saved my day
    – dekajoo
    Commented Jan 6, 2023 at 15:47
1

I was getting the same error. The reason was due to explicitly mentioning the name of the DB in the sequelize config file and it did not exist. The reason could be different in your case but a quick look at SQL Server error logs will give you the reason for the failure.

Login failed for user 'user'. Reason: Failed to open the explicitly specified database 'dbo'. [CLIENT: XX.XX.XX.XX]

Not the answer you're looking for? Browse other questions tagged or ask your own question.