我正在尝试使用一种单向关联,因为我只需要从一种模型到另一种模型的引用即可,而反之亦然。
模型艺术:
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'
}]
但仍然不起作用。
是否可以通过一种方式实现这种关联,或者我需要使用其他关联,但是如何实现呢?谢谢。
我认为您需要用softwareUsed
而不是collection
标记model
属性:
module.exports = {
attributes: {
fileName: {type: 'string', required: true},
softwareUsed: {
collection: 'Softwares'
}
}
}
sails文档中有关一对多的所有文档都涉及双向关联并添加via
属性,但我认为这种方式适用于单向关联。
当然,您的第一个api调用现在可能会更长的工作:您可能需要将单个软件ID包装在数组中。