node.js - 如何在 sails.js 模型中添加具有整数数据类型数组的属性?

标签 node.js postgresql model sails.js waterline

我将 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, or null to indicate there will be no associated record. But the specified value is not a valid user_groups. Instead of a number (the expected pk type), got: [ 2 ]

最佳答案

要在 Waterline 中进行多对一关联,语法略有不同。在该字段的对象中,您需要 collectionvia(关联集合中相应字段的名称)。所以在 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/

相关文章:

postgresql - 根据条件聚合查询 Postgres jsonb 数组

node.js - 无法使用 npm 运行任何子进程

node.js - Mongoose 中 document.save() 后的人口数量错误

ruby-on-rails - 在分离的 Rails 应用程序中使用 Sequel 访问 postgres 数据库

PostgreSQL TDE 支持

c# - 触发 OnPropertyChanged 时更新其他属性

c# - 使用 ASP .NET MVC 模型值来验证另一个模型值?

ios - 与iOS开发相比,Web开发中的模型(MVC)有什么区别

node.js - 文件 createWriteStream 中的 NodeError [ERR_STREAM_WRITE_AFTER_END] : write after end,

javascript - Node.js 16 -> 17 更改了 "localhost"的分辨率?