Sails.js sails js中引用id数组的单向关联
我尝试使用单向关联,因为我只需要从一个模型引用到另一个模型,而不需要从另一个模型引用到另一个模型 模特艺术:Sails.js sails js中引用id数组的单向关联,sails.js,waterline,sails-mongo,Sails.js,Waterline,Sails Mongo,我尝试使用单向关联,因为我只需要从一个模型引用到另一个模型,而不需要从另一个模型引用到另一个模型 模特艺术: module.exports = { attributes: { fileName: {type: 'string', required: true}, softwareUsed: { model: 'Softwares' } } } 模型软件: module.exports = { attributes: { name: {type: 'string',
module.exports = {
attributes: {
fileName: {type: 'string', required: true},
softwareUsed: {
model: 'Softwares'
}
}
}
模型软件:
module.exports = {
attributes: {
name: {type: 'string', required: true}
}
}
这是我的api:
http://localhost:1337/api/v1/arts/create
如果这是我的请求主体,它可以正常工作:
request body:
{
"fileName": "booking.jpeg",
"softwareUsed": "5e70309cbf12b61299d6c528",
}
但我想存储所用软件的数组,所以我尝试:
request body:
{
"fileName": "booking.jpeg",
"softwareUsed": ["5e70309cbf12b61299d6c528", "5e70309cbf12b61299d6c529"],
}
但我有一个错误:
error: OperationalError [UsageError]: Invalid new record.
Details:
Could not use specified `softwareUsed`. Expecting an id representing the associated record, or `null` to indicate there will be no associated record. But the specified value is not a valid `softwareUsed`. Instead of a string (the expected pk type), the provided value is: [ '5e70309cbf12b61299d6c528', '5e70309cbf12b61299d6c529' ]
我还尝试在模型中创建阵列:
softwareUsed: [{
model: 'Softwares'
}]
但还是不行
有没有一种方法可以通过一种方式关联,或者我需要使用另一种关联,但是我如何才能实现呢?
谢谢。我认为您需要使用
集合
而不是型号
来标记软件使用
属性:
module.exports = {
attributes: {
fileName: {type: 'string', required: true},
softwareUsed: {
collection: 'Softwares'
}
}
}
sails文档中关于一对多的所有文档都涉及双向关联,并通过属性添加一个,但我认为这种方法适用于单向关联
当然,您的第一个api调用现在可能工作时间更长:您可能需要将单个软件id包装在一个数组中。您的意思是这样吗?“使用的软件”:[“5e70309cbf12b61299d6c528”],[“5e70309cbf12b61299d6c529”],不,不是使用的软件:[“5e70309cbf12b61299d6c528”
或使用的软件:[“5e70309cbf12b61299d6c528”,“5e70309cbf12b61299d6c529”
。正如我在帖子中所说,这不起作用:使用的软件:[“5E70309CBF1299CBF1299D6C528”,“5E61299C529”]这不会导致错误,但所使用的软件不会存储在数据库中。只有其他字段。@noyruto88在更新模型以使用其连接属性中的collection
而不是model
后,您是否尝试过这种格式?