title: How do I properly map attributes of relations in sequelize.js?
body: <p>I'm creating a recipe-database (commonly known as a cookbook) where I need to have a many-to-many relationship between ingredients and recipes and I'm using <a href="/questions/tagged/sequelize.js" class="post-tag" title="show questions tagged &#39;sequelize.js&#39;" rel="tag">sequelize.js</a> in combination with <a href="/questions/tagged/postgresql" class="post-tag" title="show questions tagged &#39;postgresql&#39;" rel="tag">postgresql</a>.</p>
  
  <p>When an ingredient is added to a recipe I need to declare the correct amount of that ingredient that goes into the recipe.</p>
  
  <p>I've declared (reduced example)</p>
  
  <pre><code>var Ingredient = sequelize.define('Ingredient', {
      name: Sequelize.STRING
  }, {
      freezeTable: true
  });
  
  var Recipe = sequelize.define('Recipe', {
      name: Sequelize.STRING
  }, {
      freezeTable: true
  });
  
  var RecipeIngredient = sequelize.define('RecipeIngredient', {
      amount: Sequelize.DOUBLE
  });
  
  Ingredient.belongsToMany(Recipe, { through: RecipeIngredient });
  Recipe.belongsToMany(Ingredient, {
      through: RecipeIngredient,
      as: 'ingredients'
  });
  </code></pre>
  
  <p>My problem is with how data is returned when one my REST endpoints do</p>
  
  <pre><code>router.get('/recipes', function(req, res) {
      Recipe.findAll({
          include: [{
              model: Ingredient,
              as: 'ingredients'
           }]
      }).then(function(r) {
          return res.status(200).json(r[0].toJSON());
      })
  });
  </code></pre>
  
  <p>The resulting JSON that gets sent to the client looks like this (timestamps omitted):</p>
  
  <pre><code>{
    "id": 1,
    "name": "Carrots",
    "ingredients": [
      {
        "id": 1,
        "name": "carrot",
        "RecipeIngredient": {
          "amount": 12,
          "RecipeId": 1,
          "IngredientId": 1
        }
      }
    ]
  }
  </code></pre>
  
  <p>While all I wanted was</p>
  
  <pre><code>{
    "id": 1,
    "name": "Carrots",
    "ingredients": [
      {
        "id": 1,
        "name": "carrot",
        "amount": 12,
      }
    ]
  }
  </code></pre>
  
  <p>That is, I want the <code>amount</code> field from the relation-table to be included in the result instead of the entire <code>RecipeIngredient</code> object.</p>
  
  <p>The database generated by sequelize looks like this:</p>
  
  <pre>
  Ingredients
  id  name
  1   carrot
  
  Recipes
  id  name
  1   Carrots
  
  RecipeIngredients
  amount  RecipeId  IngredientId
  12      1         1
  </pre>
  
  <p>I've tried to provide an <code>attributes</code> array as a property to the <code>include</code> like this:</p>
  
  <pre><code>include: [{
      model: Ingredient,
      as: 'ingredients',
      attributes: []
   }]
  </code></pre>
  
  <p>But setting either <code>['amount']</code> or <code>['RecipeIngredient.amount']</code> as the attributes-value throws errors like</p>
  
  <blockquote>
    <p>Unhandled rejection SequelizeDatabaseError: column ingredients.RecipeIngredient.amount does not exist</p>
  </blockquote>
  
  <p>Obviously I can fix this in JS using <code>.map</code> but surely there must be a way to make sequelize do the work for me? </p>