22

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;

3 Answers 3

61

I'm going to answer myself as it might be useful to someone in future. So according to #3664, #2974 and #2975 the answer is the following (thanks to mickhansen):

include: [{
  model: dbcontext.item,
  as: 'items',
  attributes: ['spn'],
  through: {
    attributes: []
  }        
}]

And soon it will be documented.

6
  • For the life of me I cannot get this to work... are we sure this works? Mar 15, 2016 at 2:44
  • Worked for me @MirroredFate, if you still have it, post your code on pastebin and lets have a look at it. May 4, 2016 at 16:32
  • @GustavoMeira There is already an issue on it here: github.com/sequelize/sequelize/issues/5590 May 4, 2016 at 17:22
  • 3
    I just did empty attributes at the model level and seemed to exclude it in the projection
    – Joey V.
    May 17, 2016 at 22:46
  • It seems it still includes them in the select statement, just removed them in final output...
    – Mark Ni
    Feb 4, 2017 at 7:22
3

I realize this thread is a bit outdated, but since this is high in the Google search results and I struggled to find the answer myself, I thought I'd add this here.

If you're using Model.getAssociatedModel() or Model.$get() (for sequelize-typescript), the current answers listed will not work for this use case. In order to hide the model associations you need to add joinTableAttributes: []

Example:

Model.getAssociatedModel({
  joinTableAttributes: []
})

Example:

Model.$get('property', <any>{
  joinTableAttributes: []
});

At the time of this post, joinTableAttributes is not included in the sequelize-typescript types hence the <any>

1
through: {
attributes: []
}   

works for me

1
  • 4
    Please elaborate on your answer and how it applies to the original question. The answer should include enough information to present a complete solution. Dec 28, 2018 at 6:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.