【问题标题】:Sails v1.0: error while using custom primary key with mongoSails v1.0:在 mongo 中使用自定义主键时出错
【发布时间】:2017-03-24 15:43:40
【问题描述】:

我正在尝试 SailsJS (v1.0.0-32) 的 beta 版本,但在配置自定义 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.js sails-mongo


    【解决方案1】:

    为了在 Sails 1.0 中使用带有 sails-mongo 的非 ObjectID 主键,您必须在模型中设置 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 开始实施的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-12
      • 1970-01-01
      • 2016-05-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多