Search code examples
node.jspostgresqlsails.jswaterlinesails-postgresql

SailsJS/Waterline cannot create models in Postgres


Im trying to deploy my Sails JS application onto a standard Ubuntu 14.04.3 VM.

Right now, Im having trouble with connecting my SailsJS instance with PostgreSQL server instance. Both the SailsJS server and the PostgreSQL server are running in the same VM.

I am getting the following output from my Sails JS log:

MySailsServer-0 Sending 500 ("Server Error") response:
 Error (E_UNKNOWN) :: Encountered an unexpected error
error: relation "recording" does not exist
    at Connection.parseE (/home/vmuser/MySailsServer/node_modules/sails-postgresql/node_modules/pg/lib/connection.js:539:11)
    at Connection.parseMessage (/home/vmuser/MySailsServer/node_modules/sails-postgresql/node_modules/pg/lib/connection.js:366:17)
    at Socket.<anonymous> (/home/vmuser/MySailsServer/node_modules/sails-postgresql/node_modules/pg/lib/connection.js:105:22)
    at emitOne (events.js:77:13)
    at Socket.emit (events.js:169:7)
    at readableAddChunk (_stream_readable.js:146:16)
    at Socket.Readable.push (_stream_readable.js:110:10)
    at TCP.onread (net.js:523:20)

It seems as though my Recording model is not being created in the database. I checked the Postgre logs for more info. Here's the output of cat /var/log/postgresql/postgresql-9.3-main.log:

2015-10-17 14:46:13 CDT ERROR:  relation "recording" does not exist at character 491
2015-10-17 14:46:13 CDT STATEMENT:  SELECT "recording"."startTime", "recording"."endTime", "recording"."filename", "recording"."id", "recording"."createdAt", "recording"."updatedAt", "recording"."section", "__section"."startTime" AS "section___startTime", "__section"."endTime" AS "section___endTime", "__section"."name" AS "section___name", "__section"."id" AS "section___id", "__section"."createdAt" AS "section___createdAt", "__section"."updatedAt" AS "section___updatedAt", "__section"."course" AS "section___course" FROM "recording" AS "recording"  LEFT OUTER JOIN "section" AS "__section" ON "recording"."section" = "__section"."id"  LIMIT 30 OFFSET 0

Any clue on why none of the models I have declared under api/models/ are being made into tables in PostgreSQL?

Here is the configuration I have under config/connections.js:

  postgresqlServer: {
    adapter: 'sails-postgresql',
    host: 'localhost',
    user: <username>,
    password: <password>,
    database: <db_name>,
    schema: true
  }

Under config/models.js I have put:

module.exports.models = {
  connection: 'postgresqlServer',
  migrate: 'alter'
};

I can't see what Im doing incorrectly. It seems like my SailsJS Models aren't being translated into PostgreSQL tables and Im not sure why. Any help is greatly appreciated!


Solution

  • If you're lifting in production mode with sails lift --prod, sails forces waterline into 'safe' mode. In safe mode, no database alterations are done, including the initial setup of tables.

    To get the models to build beforehand, you need to run sails lift without the prod flag. Ideally this is part of your build process, if you use a deploy script.