Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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;
like image 478
Andrey Borisko Avatar asked May 06 '15 16:05

Andrey Borisko


3 Answers

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.

like image 120
Andrey Borisko Avatar answered Nov 01 '22 23:11

Andrey Borisko


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>

like image 43
Santiago Ramirez Avatar answered Nov 01 '22 23:11

Santiago Ramirez


through: {
attributes: []
}   

works for me

like image 1
Cherishh Avatar answered Nov 01 '22 23:11

Cherishh