question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't exclude association's fields from select statement in sequelize

See original GitHub issue

I have the following code (simplified):

var group = sequelize.define("group", {
    id: {type: DataTypes.INTEGER, autoIncrement: false, primaryKey: true},
    name: type: DataTypes.STRING,
    parentId: DataTypes.INTEGER
}, { classMethods: {
        associate: function (models) {
            group.belongsToMany(models.item, { as:'items', foreignKey: 'group_id', through: models.group_item_tie });
        }}
});

var group_item_tie = sequelize.define("group_item_tie", {}, {freezeTableName: true});

var item = sequelize.define("item", {
    spn: { type: DataTypes.INTEGER, autoIncrement: false, primaryKey: true },
}, { classMethods: {
        associate: function (models) {
            item.belongsToMany(models.group, { foreignKey: 'spn', through: models.group_item_tie });
        }}
});

When I try to return some records with relationships, let’s say like this:

dbcontext.group.findAll({
    where: { id: 6 },
    include: [{
                model: dbcontext.item,
                as: 'items',
                attributes: ['spn']
            }]
    })

I also get in result the fields from a tie table group_item_tie:

[{
    "id": 6,
    "name": "abc",
    "parentId": 5,
    "createdAt": "2015-05-06T15:54:58.000Z",
    "updatedAt": "2015-05-06T15:54:58.000Z",
    "items": [
        {   "spn": 1,
            "group_item_tie": {
                "createdAt": "2015-05-06 15:54:58.000 +00:00",
                "updatedAt": "2015-05-06 15:54:58.000 +00:00",
                "group_id": 6,
                "spn": 1
            }
        },
        {   "spn": 2,
            "group_item_tie": {
                "createdAt": "2015-05-06 15:54:58.000 +00:00",
                "updatedAt": "2015-05-06 15:54:58.000 +00:00",
                "group_id": 6,
                "spn": 2
            }
        },

I see it in generated sql query. How to exclude those from select statement? I’ve tried a few other things but was not successful.

I hope there is something cleaner then just doing:

delete item.group_item_tie;

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:3
  • Comments:18 (3 by maintainers)

github_iconTop GitHub Comments

90reactions
mickhansencommented, May 7, 2015
include: [{
  model: dbcontext.item,
  as: 'items',
  attributes: ['spn'],
  through: {
    attributes: []
  }        
}]
12reactions
kuubsoncommented, Jan 12, 2020

just use joinTableAttributes attribute

await user.getCars({
    joinTableAttributes: [] 
})
Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't exclude association's fields from select statement in ...
I have the following code (simplified): var group = sequelize.define( ...
Read more >
Can't exclude association's fields from select statement in ...
Coding example for the question Can't exclude association's fields from select statement in sequelize-sequelize.js.
Read more >
Associations - Sequelize
The A.belongsToMany(B, { through: 'C' }) association means that a Many-To-Many relationship exists between A and B , using table C as junction ......
Read more >
How To Use Sequelize with Node.js and MySQL - DigitalOcean
Then, you will create Sequelize associations for one-to-one, ... With this call, Sequelize will automatically perform an SQL query to the ...
Read more >
Handling sensitive fields with sequelize.js - DEV Community ‍ ‍
Alternatives to exclude certain fields from query results when using ... with a few things like TypeScript support and associations (so many ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found