【问题标题】:How to create a list of objects which are one-way associated with objects of another model in SailsJS?如何在 SailsJS 中创建与另一个模型的对象单向关联的对象列表?
【发布时间】:2017-01-31 20:52:53
【问题描述】:

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

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

【问题讨论】:

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


    【解决方案1】:

    将模型更改为集合:

    users_who_can_answer_comments: {
        collection: 'user'
    }
    

    【讨论】:

    • 那是不是就跟一对多一样,从用户端不提播放列表?
    • 是的,正是 ;)
    • 我也可以使用viathrough 属性来创建关联模型吗?否则,它会创建一个名称很长的表 ('playlist_users_who_can_answer_cmets__user_users_who_can_answer_cmets_user') 并给出错误。 error: Error (E_UNKNOWN) :: Encountered an unexpected error : ER_TOO_LONG_IDENT: Identifier name 'playlist_users_who_can_answer_comments__user_users_who_can_answer_comments_user' is too long
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-14
    • 2015-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-23
    • 2015-04-12
    相关资源
    最近更新 更多