Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Id not returned on Model.create() #6052

Closed
randallmeeker opened this issue Jun 18, 2014 · 4 comments
Closed

Id not returned on Model.create() #6052

randallmeeker opened this issue Jun 18, 2014 · 4 comments

Comments

@randallmeeker
Copy link

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

@randallmeeker
Copy link
Author

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.

@particlebanana
Copy link
Contributor

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
}

@particlebanana
Copy link
Contributor

Also doc updates would be great!

@randallmeeker
Copy link
Author

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.

@johnabrams7 johnabrams7 transferred this issue from balderdashy/waterline May 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants