【问题标题】:Sails-mongo adapter: Unexpected error from database adapter: Invalid primary key value provided for `id`. Cannot interpret `94...` as a Mongo idSails-mongo 适配器:来自数据库适配器的意外错误:为“id”提供的主键值无效。无法将 `94...` 解释为 Mongo id
【发布时间】:2020-06-07 02:14:16
【问题描述】:

我的sails 应用程序在连接到mysql 数据库时运行良好。然而,我需要切换到 mongoDB,因为它是项目的要求,这就是我面临的问题。

我的模型与其工作有一些关系,为了实现这一点,我必须进行一些修改,其中包括:我使用uuid 为每个模型设置主键 (id)(id 是自动生成的由 mysql 之前)。但是,当我尝试向服务器提交请求时,我遇到了错误:

AdapterError: Unexpected error from database adapter: Invalid primary key value provided for `id`.  Cannot interpret `9456b206-ebcf-4a6d-b81c-93964c027f04` as a Mongo id.
(Usually, this is the result of a bug in application logic.)

这是我的一个模型的示例 -pick.js:

module.exports = {
  
  attributes: {
    userId: {
      type: 'string',
      required: true,
      //unique: true,
    },
    comment:{
      type: 'string',
      required: true
    },
    createdBy:{
      type: 'string',
      required: true
    },
    info: {
      model: 'personalinfo' 
    },
    admin: {
      model: 'admin'
    },

    id:{
      type: 'string',
    },

  },

};

【问题讨论】:

标签: node.js mongodb sails.js


【解决方案1】:

我找到了解决问题的捷径。我不必使用 uuid 来设置主键。 Sails 已经在 config.models.js 中使用下面的 sn-p 连接 mongo db 并方便地设置主键。

attributes: {
  ...
  //id: { type: 'number', autoIncrement: true, },
  id: { type: 'string', columnName: '_id' },
  ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多