title: Querying association tables in Sequelize
body: <p>I have two tables (<code>users</code> and <code>games</code>) joined by an association table (<code>game_players</code>), creating a many-to-many relationship:</p>
  
  <pre><code>models.Game.belongsToMany(models.User, { through: models.GamePlayer, as: 'players' });
  models.User.belongsToMany(models.Game, { through: models.GamePlayer, foreignKey: 'user_id' });
  </code></pre>
  
  <p>In addition to the foreign keys <code>user_id</code> and <code>game_id</code>, <code>game_players</code> has a few extra columns for link-specific data:</p>
  
  <pre><code>sequelize.define('game_player', {
      isReady: {
          defaultValue: false,
          type: Sequelize.BOOLEAN,
          field: 'is_ready'
      },
      isDisabled: {
          defaultValue: false,
          type: Sequelize.BOOLEAN,
          field: 'is_disabled'
      },
      powerPreferences: {
          type: Sequelize.TEXT,
          field: 'power_preferences'
      },
      power: {
          type: Sequelize.STRING(2),
          defaultValue: '?'
      }
  }, {
      underscored: true
  });
  </code></pre>
  
  <p>Suppose I want to fetch a game and eagerly load <em>active</em> players. This was my first effort:</p>
  
  <pre><code>db.models.Game.findAll({
      include: [{
          model: db.models.User,
          as: 'players',
          where: { 'game_player.isDisabled': false }
      }]
  }).nodeify(cb);
  </code></pre>
  
  <p>This generates the following SQL, which throws the error <code>Column players.game_player.isDisabled does not exist</code>:</p>
  
  <pre><code>SELECT "game"."id", 
     "game"."name", 
     "game"."description", 
     "game"."variant", 
     "game"."status", 
     "game"."move_clock"                       AS "moveClock", 
     "game"."retreat_clock"                    AS "retreatClock", 
     "game"."adjust_clock"                     AS "adjustClock", 
     "game"."max_players"                      AS "maxPlayers", 
     "game"."created_at", 
     "game"."updated_at", 
     "game"."gm_id", 
     "game"."current_phase_id", 
     "players"."id"                            AS "players.id", 
     "players"."email"                         AS "players.email", 
     "players"."temp_email"                    AS "players.tempEmail", 
     "players"."password"                      AS "players.password", 
     "players"."password_salt"                 AS "players.passwordSalt", 
     "players"."action_count"                  AS "players.actionCount", 
     "players"."failed_action_count"           AS "players.failedActionCount", 
     "players"."created_at"                    AS "players.created_at", 
     "players"."updated_at"                    AS "players.updated_at", 
     "players.game_player"."is_ready"          AS 
     "players.game_player.isReady", 
     "players.game_player"."is_disabled"       AS 
     "players.game_player.isDisabled", 
     "players.game_player"."power_preferences" AS 
     "players.game_player.powerPreferences", 
     "players.game_player"."power"             AS "players.game_player.power", 
     "players.game_player"."created_at"        AS 
     "players.game_player.created_at", 
     "players.game_player"."updated_at"        AS 
     "players.game_player.updated_at", 
     "players.game_player"."game_id"           AS 
     "players.game_player.game_id", 
     "players.game_player"."user_id"           AS 
     "players.game_player.user_id" 
  FROM   "games" AS "game" 
     INNER JOIN ("game_players" AS "players.game_player" 
                 INNER JOIN "users" AS "players" 
                         ON "players"."id" = "players.game_player"."user_id") 
             ON "game"."id" = "players.game_player"."game_id" 
                AND "players"."game_player.isdisabled" = false;
  </code></pre>
  
  <p>Clearly Sequelize is wrapping my constraint alias with incorrect quotes: 'players'.'game_player.isdisabled' should be 'players.game_player'.isdisabled. How can I revise my Sequelize code above to correctly query this column?</p>