Siddhartha Chowdhury
Siddhartha Chowdhury

Reputation: 2732

Sails.js ID not returned on Model.create()

js and am using MySQL along with it.

So in one of my controller I am trying to create a row, which is working fine but I was wondering why am I not getting back particularly the ID of the row along with other details.

Here's how my Model(Location) looks like

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

    attributes: {
        lat:{
            type: 'float',
            required: true
        },
        lng:{
            type: 'float',
            required: true
        },
        id: {
           type: 'integer', 
           primaryKey: true,
        }
    }
};

And my LocationController:

Locations.create(req.params.all(), function(err, loc){
    if(err) return res.negotiate(err)
    res.status(201);
    return res.json(loc);
})

The response am getting is:

{
  "lat": 19.075984,
  "lng": 72.877656
}

I need the ID of the table row along with the lat and lng

Upvotes: 0

Views: 1078

Answers (1)

Alex
Alex

Reputation: 713

This is Waterline ORM issues:

Please checkout https://github.com/balderdashy/waterline/issues/481 may be helpful

This is because you have the autoPK: false flag set. So you could either turn that back on and remove the id field from your attributes or add autoIncrement: true to the id attribute.

The autoPK flag builds an id attribute identical to what you are building:

id: { type: 'integer', primaryKey: true, autoIncrement: true }

Upvotes: 2

Related Questions