balderdashy / balderdashy/sails

How to fix 'When using `sails-mongo`, primary keys MUST have `columnName: '_id'`'

Open
#6,540 11 comments 0 reactions 0 assignees View on GitHub
mongo orm repro please
Dominant language
JavaScript
Stars
22.8k
Forks
1.9k
PR merge metrics
No merged PRs in 30d

Description

**Node version**: 10.15.3
**Sails version** _(sails)_: 1.2.1
**sails-mongo**: 1.0.1



when i get documents of a mongodb collection, i've got error like this:

> In model `archive`:
> debug: The default primary key attribute (`id`) is not set up correctly.
> debug: When using `sails-mongo`, primary keys MUST have `columnName: '_id'`,
> debug: and must _not_ have `autoIncrement: true`.
> debug: Also, in most cases (unless `dontUseObjectIds` has been set to `true` for the model),
> debug: then the `type` of the primary key must also be `string`.

**sails.config.datastores.js**
```
module.exports.datastores = {
default: {
adapter: 'sails-mongo',
host: 'localhost',
port: 27017,
database: 'test',
}
};
```
**sails.config.models.js**
```
module.exports.models = {

migrate: 'safe',
attributes: {
createdAt: { type: 'number', autoCreatedAt: true, },
updatedAt: { type: 'number', autoUpdatedAt: true, },
id: { type: 'string', columnName: '_id' },
},
dataEncryptionKeys: {
default: 'RWcFGJN8+at5E7eIwNCIQxkR7P0nRAW8Fg4c9tzwFTw='
},
cascadeOnDestroy: true
};
```
**api.models.User.js**
```
module.exports = {

tableName: 'user',
attributes: {
name: {
type: 'string'
},
age: {
type: 'number',

}
},

};
```
i've got error when i run **api.controllers.UserController.js**
```
module.exports = {
getUsers: function (req, res) {
let users = User.find();
return res.send(users);
},
};
```

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the error from api.controllers.UserController.js with the shown sails.config.models.js and api.models.User.js configuration. Read the Sails model and sails-mongo primary-key validation around User.find(), then verify the reported configuration is accepted and the query returns users without the primary-key error.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, mongodb, node.js
Domain
backend, databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.