我将 sails 与 postgresql 一起使用,并且我有一个具有一组组 ID 的用户。我想将组 ID 保存在用户表中。我的用户模型是这样的:
module.exports = {
attributes: {
first_name: {
type: 'string'
},
last_name: {
type: 'string'
},
company: {
type: 'string'
},
password: {
type: 'string',
minLength: 6,
required: true,
columnName: "password"
},
user_groups: {
model: 'groups',
}
}
但是当我发送像 [2] 这样的请求时,我收到以下错误:
Could not use specified
user_groups
. Expecting an id representing the associated record, ornull
to indicate there will be no associated record. But the specified value is not a validuser_groups
. Instead of a number (the expected pk type), got: [ 2 ]
最佳答案
要在 Waterline 中进行多对一关联,语法略有不同。在该字段的对象中,您需要 collection
和 via
(关联集合中相应字段的名称)。所以在 User
中(为了约定,我将模型名称设为单数和 PascalCase)它会是这样的:
user_groups: {
collection: 'Group',
via: 'users'
}
在 Group
模型中,您有:
users: {
collection: 'User',
via: 'user_groups'
}
关于node.js - 如何在 sails.js 模型中添加具有整数数据类型数组的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52495963/