2

I have 2 tables post and tags. I'm using Tag to get all the posts associated with it.

models.Tag.findAll({
attributes: ['tagName'],
include: [
 {model: models.Post
  attributes: ['content']
  through: {
   attributes: []
  }
 }
]
})

The problem is that it selects all the through table attributes in the query.

Although doing include.through.attributes = [] the attributes don't show up in the result query but when I console.log the select query, it's still selecting all the attributes of the through table.

Is there to exclude the through table? it makes groupBy impossible in Postgres, cuz its selecting all the columns automatically.

parwatcodes
  • 6,669
  • 5
  • 27
  • 39
Saud Punjwani
  • 477
  • 1
  • 4
  • 14
  • 1
    There is a ticket on github concerning this problem, which, as I can see, you have already commented, so I think that the solution for this issue will appear there first ;) – piotrbienias Mar 11 '17 at 12:50

1 Answers1

0

I don't reproduce on sequelize@6.5.1 sqlite3@5.0.2 with:

#!/usr/bin/env node

// Find all posts by users that a given user follows.
// https://stackoverflow.com/questions/42632943/sequelize-multiple-where-clause

const assert = require('assert');
const path = require('path');

const { Sequelize, DataTypes } = require('sequelize');

const sequelize = new Sequelize({
  dialect: 'sqlite',
  storage: 'tmp.' + path.basename(__filename) + '.sqlite',
});

(async () => {

// Create the tables.
const User = sequelize.define('User', {
  name: { type: DataTypes.STRING },
});
const Post = sequelize.define('Post', {
  body: { type: DataTypes.STRING },
});
User.belongsToMany(User, {through: 'UserFollowUser', as: 'Follows'});
User.hasMany(Post);
Post.belongsTo(User);
await sequelize.sync({force: true});

// Create data.
const users = await User.bulkCreate([
  {name: 'user0'},
  {name: 'user1'},
  {name: 'user2'},
  {name: 'user3'},
])

const posts = await Post.bulkCreate([
  {body: 'body00', UserId: users[0].id},
  {body: 'body01', UserId: users[0].id},
  {body: 'body10', UserId: users[1].id},
  {body: 'body11', UserId: users[1].id},
  {body: 'body20', UserId: users[2].id},
  {body: 'body21', UserId: users[2].id},
  {body: 'body30', UserId: users[3].id},
  {body: 'body31', UserId: users[3].id},
])

await users[0].addFollows([users[1], users[2]])

const user0Follows = await User.findByPk(users[0].id, {
  attributes: [
    [Sequelize.fn('COUNT', Sequelize.col('Follows.Posts.id')), 'count']
  ],
  include: [
    {
      model: User,
      as: 'Follows',
      attributes: [],
      //through: { attributes: [] },
      include: [
        {
          model: Post,
          attributes: [],
        }
      ],
    },
  ],
})
assert.strictEqual(user0Follows.dataValues.count, 4);

await sequelize.close();
})();

The prettified generated SELECT is:

SELECT
  `User`.`id`,
  COUNT(`Follows->Posts`.`id`) AS `count`
FROM
  `Users` AS `User`
  LEFT OUTER JOIN `UserFollowUser` AS `Follows->UserFollowUser` ON `User`.`id` = `Follows->UserFollowUser`.`UserId`
  LEFT OUTER JOIN `Users` AS `Follows` ON `Follows`.`id` = `Follows->UserFollowUser`.`FollowId`
  LEFT OUTER JOIN `Posts` AS `Follows->Posts` ON `Follows`.`id` = `Follows->Posts`.`UserId`
WHERE
  `User`.`id` = 1;

If I remove the through: { attributes: [] }, then the through attributes appear, so the statement is doing something as expected:

SELECT
  `User`.`id`,
  COUNT(`Follows->Posts`.`id`) AS `count`,
  `Follows->UserFollowUser`.`createdAt` AS `Follows.UserFollowUser.createdAt`,
  `Follows->UserFollowUser`.`updatedAt` AS `Follows.UserFollowUser.updatedAt`,
  `Follows->UserFollowUser`.`UserId` AS `Follows.UserFollowUser.UserId`,
  `Follows->UserFollowUser`.`FollowId` AS `Follows.UserFollowUser.FollowId`
FROM
  `Users` AS `User`
  LEFT OUTER JOIN `UserFollowUser` AS `Follows->UserFollowUser` ON `User`.`id` = `Follows->UserFollowUser`.`UserId`
  LEFT OUTER JOIN `Users` AS `Follows` ON `Follows`.`id` = `Follows->UserFollowUser`.`FollowId`
  LEFT OUTER JOIN `Posts` AS `Follows->Posts` ON `Follows`.`id` = `Follows->Posts`.`UserId`
WHERE
  `User`.`id` = 1;

so likely this was fixed.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985