sails.jssails-mongosails-postgresql

SailsJS - Error occurred connecting to multiple database: error: syntax error at or near "SELEC"


I have MongoDB used in my application, now I need to connect to Postgresql to get some other data. But after I add connection and model for Postgresql, there's an error occurred when I started the application with sails lift. Following are the whole error message:

error: A hook (`orm`) failed to load!
error: Error (E_UNKNOWN) :: Encountered an unexpected error
error: syntax error at or near "SELEC"
  at Connection.parseE (/Users/xx/Documents/xx/Code/services/node_modules/sails-postgresql/node_modules/pg/lib/connection.js:539:11)
  at Connection.parseMessage (/Users/xx/Documents/xx/Code/services/node_modules/sails-postgresql/node_modules/pg/lib/connection.js:366:17)
  at Socket.<anonymous> (/Users/xx/Documents/xx/Code/services/node_modules/sails-postgresql/node_modules/pg/lib/connection.js:105:22)
  at emitOne (events.js:96:13)
  at Socket.emit (events.js:188:7)
  at readableAddChunk (_stream_readable.js:172:18)
  at Socket.Readable.push (_stream_readable.js:130:10)
  at TCP.onread (net.js:535:20)

All the changes I made comparing the working application are:

  1. Added connection: 'mongodb' in models.js to set MongoDB as the default DB connection.
  2. Add connection: 'postgresdb' in my new created model (ActivityController) for connecting to Postgresql.

It took me a lot of time working on this. As my investigation and learning on the documentation, seems all configuration things are okay. Did I miss anything?

I'd be appreciated if anyone can help.


Solution

  • Finally, I found the root cause, almost breakdown. Because the default connection is MongoDB, and the value of migrate is set to alter, so the model using PostgreSQL using this auto-migrate strategy. That's why it's trying to manipulate the DB, which can be inferred from the error message actually.

    So it worked after I added migrate: safe to the model connecting to PostgreSQL.

    Following is the Auto-migrate strategy from SailsJS document.

    Auto-migration strategy | Description

    safe                 | never auto-migrate my database(s). I will do it myself, by hand.
    
    alter                | auto-migrate columns/fields, but attempt to keep my existing data (experimental)
    
    drop                 | wipe/drop ALL my data and rebuild models every time I lift Sails