Sails v1.0: error when using custom primary key with mongo

I'm trying to run a beta version of SailsJS (v1.0.0-32) and I'm having some problems setting up a custom id. Below you will find my current configuration:

modelExample.js

module.exports = {

  attributes: {
    id:{
      type: 'string',
      columnName: '_id'
    },
    attr: {
      type: 'number'
    }
  }
}

      

Model configuration config/models.js

attributes: {
    createdAt: { type: 'number', autoCreatedAt: true, },
    updatedAt: { type: 'number', autoUpdatedAt: true, },
    id: { type: 'string', columnName: '_id' },
}

      

The element to be inserted:

{id:"600000", attr:40}

error

I get when trying to create a record with an "id" attribute included on the element that is trying to be created:

AdapterError: Unexpected error from database adapter: Invalid primary key value provided for `id`.  Cannot interpret `600000` as a Mongo id.
(Usually, this is the result of a bug in application logic.)

      

It seems that mongo doesn't like the string 600000

like id

, but I'm not sure, maybe I don't understand something related to IDs in mongo. In the old version of sails, I never had this problem as overriding id was simple.

For more information version of mongo sail adapter: "sails-mongo": "^1.0.0-5"

+3


source to share


1 answer


To use non-ObjectID primary keys with sails-mongo

in Sails 1.0, you must set dontUseObjectIds: true

in your model, for example:

// api/models/User.js
module.exports = {
  dontUseObjectIds: true,
  attributes: {
    id: { type: 'number', columnName: '_id' }, // <-- still need to set `columnName`!
    name: { type: 'string' },
    ...etc...
  }
}

      



This is implemented as sails-mongo

v1.0.0-7.

0


source







All Articles