Sails: Id not returned on Model.create()

Created on 18 Jun 2014  路  4Comments  路  Source: balderdashy/sails

I posted in the groups, but I think its a bug assuming create is supposed to return the id (or full model) of the record created. When I try and perform a create on this model I don't not get an instance back. All that is returned are those fields that are sent with the create method. This means I am not getting the ID of the record created returned in the callback. My ID is autoIncrement in MySQL

I'm using
sails 0.10.0-rc8
sails-mysql 0.10.0-rc7

This is my model

account.js
module.exports = {
  migrate: 'safe',
  tableName: 'accounts',
  autoPK: false,
  attributes: {
        id: {
                        type: 'INTEGER', 
            primaryKey: true
            },
        companyId: 'INTEGER',
        name: {type: 'STRING', maxLength: 100},
        notes: {type: 'STRING', maxLength: 255},
        createdAt: 'DATETIME',
        updatedAt: 'DATETIME',
        deletedAt: 'DATETIME',

        // Associations
        company: {
            model: 'company',
            columnName: 'companyId'
        },
        assignments: {
            collection: 'assignment',
            via: 'account'
        },
        lastAssignment:{
            model: 'assignment',
            columnName: 'lastAssignmentId'
        }
    }
};

This is a few console.log()s that I dropped into blueprints/create.js and to show the I/O

Params send to Sails  -------------------------
{ companyId: 1, name: 'Account Name' }

Data Sent to Model.create() --------------------
{ companyId: 1,
  name: 'Account Name',
  createdAt: Tue Jun 17 2014 21:29:21 GMT-0600 (Mountain Daylight Time),
  updatedAt: Tue Jun 17 2014 21:29:21 GMT-0600 (Mountain Daylight Time) }

Instance returned from create -------------------------
{ name: 'Account Name',
  createdAt: Tue Jun 17 2014 21:29:21 GMT-0600 (Mountain Daylight Time),
  updatedAt: Tue Jun 17 2014 21:29:21 GMT-0600 (Mountain Daylight Time),
  company: 1 }

And then I also get this error as well at the end of the create action.

error: Invalid usage of publishCreate() :: Values must have an `id`, instead got
 ::

This is the result of all tested create actions. At the end of the cycle the record is entered into MySQL with the MySQL generated ID. I also tested this directly from a console> model.create() with the same results

Most helpful comment

as you can see by my model has migrate safe. I'm working with an already existing dataset. So autoPk is false, but I'm not sure if autoPK false does anything since I have migrate = safe.

I think where the sails/docs lack are mostly in the area of those of us dealing with existing DB's. Sails seems to work great out of the box, but there are many many hidden tweaks if your bringing in an existing DB.

All 4 comments

Victory is mine and thank god for node inspector

It looks like autoIncrement: true is needed in order for waterline to return the new ID value.

I would be happy to amend docs and create a pull request to give back to the cause, but I'm unsure if this is a bug or if this is how it should be.

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
}

Also doc updates would be great!

as you can see by my model has migrate safe. I'm working with an already existing dataset. So autoPk is false, but I'm not sure if autoPK false does anything since I have migrate = safe.

I think where the sails/docs lack are mostly in the area of those of us dealing with existing DB's. Sails seems to work great out of the box, but there are many many hidden tweaks if your bringing in an existing DB.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kesavkolla picture kesavkolla  路  4Comments

Alirezamohammadi picture Alirezamohammadi  路  4Comments

danil-z picture danil-z  路  3Comments

JohnGrisham picture JohnGrisham  路  4Comments

3imed-jaberi picture 3imed-jaberi  路  3Comments