我的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.)
下面是我的一个模型--picked.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',
},
},
};
发布于 2020-06-11 18:51:26
我找到了一个解决问题的捷径。我不需要使用uuid来设置主键。Sails已经在config.models.js中使用下面的代码片段与mongo db连接并方便地设置主键。
attributes: {
...
//id: { type: 'number', autoIncrement: true, },
id: { type: 'string', columnName: '_id' },
...
}
https://stackoverflow.com/questions/62240118
复制相似问题