我正在尝试 SailsJS (v1.0.0-32) 的测试版,但在配置自定义 ID 时遇到了一些问题。波纹管你会发现我当前的配置:modelExample.js
module.exports = {
attributes: {
id:{
type: 'string',
columnName: '_id'
},
attr: {
type: 'number'
}
}
}
模型配置
config/models.js
attributes: {
createdAt: { type: 'number', autoCreatedAt: true, },
updatedAt: { type: 'number', autoUpdatedAt: true, },
id: { type: 'string', columnName: '_id' },
}
试图插入的元素:
{id:"600000", attr:40}
error
我在尝试创建包含在尝试创建的元素中的属性“id”的记录时得到: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.)
似乎 mongo 不喜欢字符串
600000
作为 id
,但我不确定我是否误解了与 mongo 中的 id 相关的内容。在旧版本的风帆中,我从来没有遇到过这个问题,因为 id 覆盖很简单。有关更多信息,sails-mongo 适配器版本为:
"sails-mongo": "^1.0.0-5"
最佳答案
为了在 sails-mongo
中使用非 ObjectID 主键在 Sails 1.0 中,您必须设置 dontUseObjectIds: true
在您的模型中,例如:
// api/models/User.js
module.exports = {
dontUseObjectIds: true,
attributes: {
id: { type: 'number', columnName: '_id' }, // <-- still need to set `columnName`!
name: { type: 'string' },
...etc...
}
}
这是从
sails-mongo
开始实现的v1.0.0-7。
关于sails.js - sails v1.0 : error while using custom primary key with mongo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43003762/