javascript - 如何在 SailsJS 中创建与另一个模型的对象单向关联的对象列表?

标签 javascript node.js orm sails.js waterline

我有模型 Playlist.jsUser.js。我希望在播放列表中有一个可以回答对他们的评论的用户列表。我不希望该属性出现在用户中。 (即单向关联)

播放列表.js

module.exports = {
    attributes: {
        id: {
            type: 'integer',
            autoIncrement: true,
            primaryKey: 
        },        
        name: {
            type: 'string',
            size: 128,
            required: true
        },
        // many to many relationship with role
        roles_with_access: {
            collection: 'role',
            via: 'playlist',
            through: 'rolehasplaylist'
        },

        // THIS ATTRIBUTE ASSOCIATE WITH ONE USER
        // BUT INSTEAD OF THAT I WANT A LIST OF USERS
        users_who_can_answer_comments: {
            model: 'user'
        }
    }
};

用户.js

module.exports = {
    attributes: {
        id: {
            type: 'integer',
            autoIncrement: true,
            primaryKey: true,
        },
        name: {
            type: 'string',
            size: 128,
            required: true
        },
        email: {
            type: 'email',
            required: true,
            unique: true
        },
        adminRole: {
            model: 'role'
        }
    }
};

Sails 文档提到了单向关联仅适用于一对一映射 - http://sailsjs.org/documentation/concepts/models-and-orm/associations/one-way-association 我想找到一种方法来获得与用户关联的用户列表?

最佳答案

将模型更改为集合:

users_who_can_answer_comments: {
    collection: 'user'
}

关于javascript - 如何在 SailsJS 中创建与另一个模型的对象单向关联的对象列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39653248/

相关文章:

javascript - 函数作为参数的 JavaScript 语法

node.js - 使用 fetch 方法从 Express 端点获取数据显示错误 React Redux

node.js - Sequelize 关系为空

asp.net-mvc-3 - 在 Entity Framework 中使用数据库优先方法

javascript - 尝试取消注册广播接收器时出现非法参数异常

javascript - 如何在 Vanilla JavaScript 中使我的自定义范围/ slider 随动器 div 与范围拇指完美居中

javascript - 如何在溢出隐藏父元素内显示绝对元素

javascript - 插入字符串而不是函数

java - 如何为 CollectionOfElements 创建自定义查询

mysql - 我可以使用MySQL建立Symfony2 ORM继承关系吗?