简体   繁体   English

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

[英]How to create a list of objects which are one-way associated with objects of another model in SailsJS?

I have models Playlist.js and User.js .我有模型Playlist.jsUser.js I want to have a list of users in Playlist who can answer for the comments on them.我想在播放列表中有一个用户列表,他们可以回答对他们的评论。 I do not want the attribute to appear in User.我不希望该属性出现在用户中。 (ie one-way-association) (即单向关联)

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

User.js用户.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 documentation have mentioned one-way association only for one-to-one mapping - http://sailsjs.org/documentation/concepts/models-and-orm/associations/one-way-association I want to find a way to have a list of users with associations to users? Sails 文档提到单向关联仅用于一对一映射 - http://sailsjs.org/documentation/concepts/models-and-orm/associations/one-way-association我想找到一种方法与用户关联的用户列表?

Change model to collection:将模型更改为集合:

users_who_can_answer_comments: {
    collection: 'user'
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM