Skip to content

Attributes query option doesn't take join table into account. #3938

Closed

Description

@einnjo

I'm trying to select some attributes on my instances before parsing them into JSON and sending them back to the client. I have a many-to-many relation and I would like to send responses to the clients without the join table.

Here's an example:

// MODELS
    var Group = sequelize.define('Group', {
        name: sequelize.Sequelize.STRING
    });

    var Person = sequelize.define('Person', {
        name: sequelize.Sequelize.STRING
    });

    var GroupPerson = sequelize.define('GroupPerson', {
        status: sequelize.Sequelize.STRING
    });

// RELATIONS
    Group.belongsToMany(Person, {through: GroupPerson, as: 'Persons'});
    Person.belongsToMany(Group, {through: GroupPerson, as: 'Groups'});

// EXAMPLE
    Person
        .findAll({
            include: [{
                model: Group,
                as: 'Groups',
                through:{
                    where: {status: 'done'}
                }
                attributes: ['id']
            }]
        });

What I'm trying to achieve:

[{id: 1}, {id: 2}]

What I'm getting right now:

[{id: 1, GroupUser: [Object]}, {id:2, GroupUser: [Object]}]

Is there a built-in way to exclude the join table object from the response?

Activity

  1. mickhansen commented on Jun 16, 2015

    @mickhansen
    Contributor

    You can use through: {attributes: []} to leave out the join table.

  2. kant-bluescape commented on Sep 3, 2015

    @kant-bluescape

    is there a way to exclude just one column from the join table such as say "password" column else I will have a very long list of attributed to include just to exclude this one column.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions