sails: Sails v1.0 & sails-mongo: Can't create a document with custom id value

From @Josebaseba on February 28, 2017 3:53

sails: v1.0.0-27 sails-mongo: 1.0.0-4

I can’t create a document with a custom id like: '58959f6f6e6d1b620acc7a00', it always gets overwritten by another “random” ID. I tried in all the possible ways but I couldn’t.

Plus, if you have your models migration option set to 'alter' the id of all the objects keep changing, so if you have a model with an association to that other document you lose it.

It took me like an hour to realize about what was happening 😃

Copied from original issue: balderdashy/sails-mongo#456

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 19 (8 by maintainers)

Commits related to this issue

Most upvoted comments

@algope This should be all set with the latest beta of Waterline and sails-mongo. The easiest way to get it in your project is:

npm cache clean
npm install sails-hook-orm@beta --force
npm install sails-mongo@beta --force

Then you should be able to set dontUseObjectIds: true in your model, and set up the id attribute however you want, e.g.:

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

Give that a whirl and let us know if it works for you!

Indeed. Thanks a lot 😃

You need to use a real mongo id, something like this: "5af2d22aa7cec4083c4c4c4d"