question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ER_NO_DB_ERROR: No database selected

See original GitHub issue

Hello.

I’m using sequelize in my Express.js application and I’ve faced with strange issue/bug. I get an exception exactly after starting of application. All database credentials are correct. It seems like sync() method is called before db connection is completely initialized or something like that. The sequelize.authenticate() method works properly.

ER_NO_DB_ERROR: No database selected
Unhandled rejection SequelizeDatabaseError: ER_NO_DB_ERROR: No database selected
    at Query.formatError (/vagrant/node_modules/sequelize/lib/dialects/mysql/query.js:175:14)
    at Query._callback (/vagrant/node_modules/sequelize/lib/dialects/mysql/query.js:49:21)
    at Query.Sequence.end (/vagrant/node_modules/mysql/lib/protocol/sequences/Sequence.js:86:24)
    at Query.ErrorPacket (/vagrant/node_modules/mysql/lib/protocol/sequences/Query.js:88:8)
    at Protocol._parsePacket (/vagrant/node_modules/mysql/lib/protocol/Protocol.js:280:23)
    at Parser.write (/vagrant/node_modules/mysql/lib/protocol/Parser.js:75:12)
    at Protocol.write (/vagrant/node_modules/mysql/lib/protocol/Protocol.js:39:16)
    at Socket.<anonymous> (/vagrant/node_modules/mysql/lib/Connection.js:103:28)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:176:18)
    at Socket.Readable.push (_stream_readable.js:134:10)
    at TCP.onread (net.js:548:20)

The problem happens in these lines of code, but it may be more deeper:

models.sequelize.sync().then(function () {
    var server = http.createServer(app);
    server.listen(configs.APP_PORT);
    server.on('error', onError);
    server.on('listening', function () {
        console.log('Listening on port ' + configs.APP_PORT);
    });
}).catch(function (error) {
    onError(error);
});

Also I get the same error when I’m trying to run migrations (sequelize db:migrate). Here is my /models/index.js

var fs = require('fs');
var path = require('path');
var Sequelize = require('sequelize');
var configs = require('../../configs');
var basename = path.basename(module.filename);
var database = {};

/**
 * Inialize models connection
 */
var sequelize = new Sequelize(configs.DATABASE_NAME, configs.DATABASE_USER, configs.DATABASE_PASSWORD, {
  host: configs.DATABASE_HOST,
  port: configs.DATABASE_PORT,
  dialect: configs.DATABASE_DIALECT,
  logging: configs.APP_DEBUG
});

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

Object.keys(database).forEach(function(modelName) {
  if (database[modelName].associate) {
    database[modelName].associate(database);
  }
});

database.sequelize = sequelize;
database.Sequelize = Sequelize;

module.exports = database;

Dialect: MariaDB Database version: 10.0.29 Sequelize version: [Node: 6.10.0, CLI: 2.7.0, ORM: 3.30.4]

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
AlpacaGoesCrazycommented, Sep 6, 2018

I am experiencing exact same issue right now, is there any solution for this?

1reaction
mmarovichcommented, Feb 23, 2019

Update on this. It seems like my colleague needed to use the dotenv-cli, and then migrate with dotenv sequelize db:migrate etc

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error 1046 No database Selected, how to resolve?
Error 1046 occurs when we miss to connect our table with a database. In this case, we don't have any database and that's...
Read more >
How to fix no database selected MySQL error
The error no database selected frequently occurs in MySQL when you perform a statement without selecting a database first.
Read more >
MySQL Error - #1046 - No database selected - Tutorialspoint
MySQL Error - #1046 - No database selected - The error-#1046 can occur when we are creating a table, but forget to select...
Read more >
MySQL No Database Selected: Actionable Tips to Fix the Error
MySQL no database selected is an error that occurs when you execute a statement without first selecting a database. The database may be...
Read more >
Error: 1046 SQLSTATE: 3D000 (ER_NO_DB_ERROR) Message
Message: No database selected. je suppose que je dois parametrer sql (choisir cette base et l'utiliser..) mais je ne sais pas comment ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found