Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find Response With Include Contains Join Table #2143

Closed
lsiv568 opened this issue Aug 12, 2014 · 12 comments
Closed

Find Response With Include Contains Join Table #2143

lsiv568 opened this issue Aug 12, 2014 · 12 comments
Labels
type: feature For issues and PRs. For new features. Never breaking changes.

Comments

@lsiv568
Copy link

lsiv568 commented Aug 12, 2014

I am using the latest version, v2.0.0-dev12, and when I perform a find with an include the returned JSON contains the join table. Based on #1468 I was under the impression that this was no longer the default behavior. I have a comment at the bottom of issue #1468 which includes more detailed information. Any idea why this is happening?

@amok
Copy link

amok commented Aug 13, 2014

+1
I have exactly same issue

@cybernadinou
Copy link

ISSUE SOLVED 👍 👯
Sorry i'm on it since 5hours xD
Solution based on your #1468

User.find({
where: {id: userId}, attributes: ['firstName', 'lastName'], 
include: [{model: db.Role, attributes: ['name', 'id']}]
}).success(function(listRole){
if(listRole){
    listRole.getRoles(
                {// delete associations tables
                  joinTableAttributes:[]}).success(function(rst){
        res.send(rst);
    });
else
    res.send(null);
}
})

I found this here : http://sequelizejs.com/docs/latest/associations#block-11-line-1
Hopes is helpful and If you could improve this solution i will be grateful too :)

@lsiv568
Copy link
Author

lsiv568 commented Aug 19, 2014

@cybernadinou Thanks for responding. This solution works but it requires another call to sequlize in order to remove the join table from the returned DAO. As my original comment stated, I was under the impression this was no longer the default behavior.

@mickhansen
Copy link
Contributor

@lsiv568 that issue/fix only deals with primary keys when using attributes, not with join table attributes i'm afraid.

On master you should be able to use include.through.attributes: [] (pseudo) - Not sure if it's on dev12

@lsiv568
Copy link
Author

lsiv568 commented Aug 19, 2014

@mickhansen Thanks for the reply. adding through.attributes: [] to my include object array for the appropriate model did the trick. FYI, it works in dev12. Thanks again!

@mickhansen
Copy link
Contributor

Great

@lsiv568
Copy link
Author

lsiv568 commented Aug 28, 2014

@lao see below.

User.find({
    where: {id: userId}, attributes: userFields,
    include: [
      {model: db.Role, attributes: roleFields, through: {attributes: []}}
    ]
});

As i'm sure you can figure out userFields and roleFields are defined outside of this call. Also, userId is passed into the function which is executing this query. Let me know if you have any questions.

@Anaphase
Copy link

Thank you jesus. Is this documented anywhere in the documentation? It took me forever to track this down!

@sushantdhiman
Copy link
Contributor

http://docs.sequelizejs.com/en/latest/docs/associations/#belongs-to-many-associations

With Belongs-To-Many you can query based on through relation and select specific attributes. For example using findAll with through

User.findAll({
  include: [{
    model: Project,
    through: {
      attributes: ['createdAt', 'startedAt', 'finishedAt'],
      where: {completed: true}
    }
  }]
});

@agchou
Copy link

agchou commented Jul 13, 2016

How would this work in N:M (2x belongsToMany) associations? Include does not work but I'm guessing you could pass something similar into user.getProjects({ through: { attributes: ['something'] } })?

@kevinvugts
Copy link

kevinvugts commented Dec 24, 2017

@agchou I totally agree with you. I am currently in the same situation where I use the provided getters and setters from Sequelize but its returning the containing join table as well.

I used:

User.findAll({
  include: [{
    model: Project,
    through: {
      attributes: ['createdAt', 'startedAt', 'finishedAt'],
      where: {completed: true}
    }
  }]
});

as well as:

user.getProjects()

But both result in the same issue.

@abhi5658
Copy link
Contributor

abhi5658 commented May 4, 2020

@agchou @kevinvugts I think you are placing the attributes at wrong place and also making the wrong updates in code as required by the solution.
You need to re-specify the attributes at the model level and then make the table attributes as none

User.findAll({
  include: [{
    model: Project,

    through: 'JoinTableAliasName' /// only if exist/needed
    as: 'modelAssociationAlias' // only if exist/needed
    
    // attributes needed from Project model/table
    attributes: ['createdAt', 'startedAt', 'finishedAt'],
    
    through: { // through again doesn't create any issue
      attributes: [], // this helps removing the join table in returned data
      where: {completed: true}
    }
  }]
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature For issues and PRs. For new features. Never breaking changes.
Projects
None yet
Development

No branches or pull requests

10 participants