1
votes

I've googled this quite a bit, and looked at other solutions on stack overflow but have found nothing that fixes my issue. When defining a model to use mysql instead of disk I get the following error

verbose: Registering model `users` in Waterline (ORM) with definition :: 
 { identity: 'users',
  tableName: 'users',
  connections: 
       [ { pool: true,
       connectionLimit: 10,
       waitForConnections: true,
       adapter: 'sails-mysql' } ],
  config: 
   { host: 'localhost',
     port: 3306,
     user: 'root',
     password: 'password',
     database: 'sailsapp' },
  attributes: { name: 'string', email: 'string', phone: 'string' },
  globalId: 'users',
  introduce: [Function: bound],
  obituary: [Function: bound],
  publish: [Function: bound],
  pluralize: [Function],
  room: [Function: bound],
  classRoom: [Function],
  subscribers: [Function],
  publishCreate: [Function: bound],
  publishUpdate: [Function: bound],
  publishDestroy: [Function: bound],
  subscribe: [Function: bound],
  unsubscribe: [Function: bound],
  adapter: [ 'adhoc_adapter_0' ] } 

Logic error in mySQL ORM.
{ [Error: ER_NO_DB_ERROR: No database selected] code: 'ER_NO_DB_ERROR', index: 0 }
error: Hook failed to load: orm (Error: ER_NO_DB_ERROR: No database selected)
verbose: Hooks loaded!
error: Error encountered while loading Sails core!
error: Error: ER_NO_DB_ERROR: No database selected
at Query.Sequence._packetToError     (/home/rich/PhpstormProjects/sailsjs/node_modules/sails-    mysql/node_modules/mysql/lib/protocol/sequences/Sequence.js:32:14)
at Query.ErrorPacket (/home/rich/PhpstormProjects/sailsjs/node_modules/sails-mysql/node_modules/mysql/lib/protocol/sequences/Query.js:82:18)
at Protocol._parsePacket (/home/rich/PhpstormProjects/sailsjs/node_modules/sails-mysql/node_modules/mysql/lib/protocol/Protocol.js:172:24)
at Parser.write (/home/rich/PhpstormProjects/sailsjs/node_modules/sails-mysql/node_modules/mysql/lib/protocol/Parser.js:62:12)
at Protocol.write (/home/rich/PhpstormProjects/sailsjs/node_modules/sails-mysql/node_modules/mysql/lib/protocol/Protocol.js:37:16)
at Socket.ondata (stream.js:51:26)
at Socket.EventEmitter.emit (events.js:117:20)
at Socket.<anonymous> (_stream_readable.js:746:14)
at Socket.EventEmitter.emit (events.js:92:17)
at emitReadable_ (_stream_readable.js:408:10)
at emitReadable (_stream_readable.js:404:5)
at readableAddChunk (_stream_readable.js:165:9)
at Socket.Readable.push (_stream_readable.js:127:10)
at TCP.onread (net.js:528:21)
--------------------
at Query.Sequence (/home/rich/PhpstormProjects/sailsjs/node_modules/sails-mysql/node_modules/mysql/lib/protocol/sequences/Sequence.js:15:20)
at new Query (/home/rich/PhpstormProjects/sailsjs/node_modules/sails-mysql/node_modules/mysql/lib/protocol/sequences/Query.js:12:12)
at Function.Connection.createQuery (/home/rich/PhpstormProjects/sailsjs/node_modules/sails-mysql/node_modules/mysql/lib/Connection.js:48:10)
at Connection.query (/home/rich/PhpstormProjects/sailsjs/node_modules/sails-mysql/node_modules/mysql/lib/Connection.js:100:26)
at __DESCRIBE__ (/home/rich/PhpstormProjects/sailsjs/node_modules/sails-mysql/lib/adapter.js:121:20)
at afterwards (/home/rich/PhpstormProjects/sailsjs/node_modules/sails-mysql/lib/adapter.js:571:7)
at Handshake._callback (/home/rich/PhpstormProjects/sailsjs/node_modules/sails-mysql/node_modules/mysql/lib/Pool.js:37:9)
at Handshake.Sequence.end (/home/rich/PhpstormProjects/sailsjs/node_modules/sails-mysql/node_modules/mysql/lib/protocol/sequences/Sequence.js:66:24)
at Handshake.Sequence.OkPacket (/home/rich/PhpstormProjects/sailsjs/node_modules/sails-mysql/node_modules/mysql/lib/protocol/sequences/Sequence.js:75:8)
at Protocol._parsePacket (/home/rich/PhpstormProjects/sailsjs/node_modules/sails-mysql/node_modules/mysql/lib/protocol/Protocol.js:172:24)
at Parser.write (/home/rich/PhpstormProjects/sailsjs/node_modules/sails-mysql/node_modules/mysql/lib/protocol/Parser.js:62:12)
at Protocol.write (/home/rich/PhpstormProjects/sailsjs/node_modules/sails-mysql/node_modules/mysql/lib/protocol/Protocol.js:37:16)
at Socket.ondata (stream.js:51:26)
at Socket.EventEmitter.emit (events.js:117:20)
at Socket.<anonymous> (_stream_readable.js:746:14)
at Socket.EventEmitter.emit (events.js:92:17)
at emitReadable_ (_stream_readable.js:408:10)
at emitReadable (_stream_readable.js:404:5)
at readableAddChunk (_stream_readable.js:165:9)
at Socket.Readable.push (_stream_readable.js:127:10)
at TCP.onread (net.js:528:21)


I have read in other places that there is a known error with the new version of sails.js (0.9.16) where the adapter and config have to be specified for each model. I have done that as you can see from the error log, however the code for my model is below

module.exports = {
    adapter: 'mysql',
    config: {
        host: 'localhost',
        port: 3306,
        user: 'root',
        password: 'password',
        database: 'sailsapp'
    },

    attributes: {
        name: 'string',
        email: 'string',
        phone: 'string'
    }
};

I'm still getting the same error. Here is the package.json file for my app

{
    "name": "sailsjs",
    "private": true,
    "version": "0.0.0",
    "description": "a Sails application",
    "dependencies": {
        "sails": "0.9.16",
        "grunt": "0.4.1",
        "sails-disk": "~0.9.0",
        "ejs": "0.8.4",
        "optimist": "0.3.4"
    },
    "scripts": {
        "start": "node app.js",
        "debug": "node debug app.js"
    },
    "main": "app.js",
    "repository": "",
    "author": "",
    "license": ""
}

and the package.json file for my version of sails-mysql

{
  "name": "sails-mysql",
  "version": "0.9.9",
  "description": "MySQL adapter for Sails.js",
  "main": "lib/adapter.js",
  "scripts": {
    "test": "make test"
  }, ...

Are there any suggestions about how to resolve this issue?

2
Duplicate question here : stackoverflow.com/questions/22548976/… - Up_One
As explained in my question this answer did not work for me I just hit this same problem. In order to get it to work, I had to add the adapter configuration information to my model, e.g. /api/models/User.js: I have already done this, I have documented that fact in my question - richbai90
Using a new app in v0.9.16, I can't replicate this. It looks like the error is happening on lift after getting a socket event; do you have a web page open that may be trying to re-connect with your server and write data as Sails is lifting? - sgress454
I'll try looking into this in the morning. I did add some logic to the on socket connect so this may be causing the issue. I'll let you know what I come up with - richbai90

2 Answers

2
votes

In v0.10, connection name can be defined on models.js which was configured on connections.js

models.js

 connection: 'someMysqlServer'

connections.js

 someMysqlServer: {
    adapter : 'sails-mysql',
    host    : 'localhost',
    user    : '',
    password: '', 
    database: ''
  }
0
votes

changing the default adapters.js file mysql adapter object from myLocalMySQLDatabase to simply mysql seemed to fix it. Don't know why they changed the name of the mysql adapter object but we're good to go now.