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

时间:2016-09-23 05:32:53

标签: javascript node.js orm sails.js waterline

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

Playlist.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'
        }
    }
};

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 我想找到一种方法来获得与用户关联的用户列表?

1 个答案:

答案 0 :(得分:1)

将模型更改为集合:

users_who_can_answer_comments: {
    collection: 'user'
}