0

I'm using sails js with the sails-postgresql adapter

I receive the following error when trying to insert a record into the database.

error: Sending 500 ("Server Error") response: 
TypeError: Object.keys called on non-object
at Function.keys (native)
at __CREATE__ (/home/user123/aoresources/node_modules/sails-postgresql/lib/adapter.js:337:16)

config/models.js:

module.exports.models = {
    autoCreatedAt: false,
    autoUpdatedAt: false,

    connection: 'postgresql',
    migrate: 'safe',
    autoPK: false
};

Table:

create table if not exists users (
    id varchar(50) not null primary key,
    email varchar(254) not null
);

User.js:

module.exports = {
    attributes: {
        email: {
            type: 'string'
        },
        id: {
            type: 'string',
            primaryKey: true
        }
    }
}

Insert code:

User.create({id: 'test', email:'something'}).exec(function createCB(err,created){});
James
  • 680
  • 2
  • 8
  • 22

1 Answers1

0

In your model User.js, add the following line: tableName: "users".

So, your User.js will look like:

module.exports = {
    tableName: "users",
    attributes: {
        email: {
            type: 'string'
        },
        id: {
            type: 'string',
            primaryKey: true
        }
    }
}

If you don't provide the tableName property, Waterline automatically takes the Model's identity as the Table name (User), but since you do not have a table named User (but users), this error occurs.

Hope this helps.

myusuf
  • 11,810
  • 11
  • 35
  • 50
  • Yeah this is what it was. I had tried it before and it didn't work, but that's because I went full retard and put the tableName inside my attributes object, lol. – James Oct 28 '14 at 17:02