title: How do I ORM additional columns on a join table in sequelize?
body: <p>I'm using node v9.5, sequelize v4.33 (postgres dialect).</p>
  
  <p>I have two first-class models: <code>Driver</code> (specific people) and <code>Car</code> (generic make+model combinations). Thus far, they've been connected by a many-to-many join table. Now I want to start tracking additional properties on that join table, but am having trouble declaring these relationships so they actually work.</p>
  
  <pre><code>const Driver = sqlz.define('Driver', {
      id: { primaryKey: true, type: DataTypes.UUID },
      name: DataTypes.string
  })
  
  const Car = sqlz.define('Car', {
      id: { primaryKey: true, type: DataTypes.UUID },
      make: DataTypes.string,
      model: DataTypes.string
  })
  
  // old associations; worked great when requirements were simpler
  Driver.belongsToMany(Car, {
      through: 'DriverCar',
      as: 'carList',
      foreignKey: 'driverId'
  })
  
  Car.belongsToMany(Driver, {
      through: 'DriverCar',
      as: 'driverList',
      foreignKey: 'carId'
  })
  </code></pre>
  
  <p>Now I want to begin tracking more information about the relationship between a car and its driver, like the color of that specific car.</p>
  
  <p>Step 1: I update the migration script, adding a new column to the join table like so:</p>
  
  <pre><code>queryInterface.createTable( 'DriverCar', {
      driverId: {
          type: sqlz.UUID,
          allowNull: false,
          primaryKey: true,
          references: {
              model: 'Driver',
              key: 'id'
          }
      },
      carId: {
          type: sqlz.UUID,
          allowNull: false,
          primaryKey: true,
          references: {
              model: 'Car',
              key: 'id'
          }
      },
      createdAt: {
          type: sqlz.DATE,
          allowNull: false
      },
      updatedAt: {
          type: sqlz.DATE,
          allowNull: false
      },
  
      // new column for join table
      color: {
        type: Sequelize.STRING
      }
  })
  </code></pre>
  
  <p>Step 2: I define a new sqlz model for <code>DriverCar</code>:</p>
  
  <pre><code>const DriverCar = sqlz.define('DriverCar', {
      color: DataTypes.string
  })
  </code></pre>
  
  <p>(I assume I only need to define the interesting properties, and that <code>driverId</code> and <code>carId</code> will still be inferred from the associations that will be defined.)</p>
  
  <p>Step 3: I need to update the associations that exist among <code>Driver</code>, <code>Car</code>, and <code>DriverCar</code>.</p>
  
  <p>This is where I'm stuck. I have attempted updating the existing associations, like so:</p>
  
  <pre><code>Driver.belongsToMany(Car, {
      through: DriverCar, // NOTE: no longer a string, but a reference to new DriverCar model
      as: 'carList',
      foreignKey: 'driverId'
  })
  
  Car.belongsToMany(Driver, {
      through: DriverCar, // NOTE: no longer a string, but a reference to new DriverCar model
      as: 'driverList',
      foreignKey: 'carId'
  })
  </code></pre>
  
  <p>This executes without error, but the new <code>color</code> property is not fetched from the join table when I try <code>driver.getCarList()</code>. (Sqlz is configured to log every SQL statement, and I have verified that no properties from the join table are being requested.)</p>
  
  <p>So, instead, I tried spelling out this relationship more explicitly, by associating <code>Driver</code> to <code>DriverCar</code>, and then <code>Car</code> to <code>DriverCar</code>:</p>
  
  <pre><code>// Driver -&gt; Car
  Driver.hasMany(DriverCar, {
      as: 'carList',
      foreignKey: 'driverId'
  })
  
  // Car -&gt; Driver
  Car.hasMany(DriverCar, {
      foreignKey: 'carId'
  })
  </code></pre>
  
  <p>I also tell sqlz that <code>DriverCar</code> won't have a standard row id:</p>
  
  <pre><code>DriverCar.removeAttribute('id')
  </code></pre>
  
  <p>At this point, requesting a Driver's carList (<code>driver.getCarList()</code>) seems to work, because I can see join table props being fetched in SQL. But saving fails:</p>
  
  <pre><code>driverModel.setCarList([ carModel1 ])
  
  UPDATE DriverCar
  SET "driverId"='a-uuid',"updatedAt"='2018-02-23 22:01:02.126 +00:00'
  WHERE "undefined" in (NULL)
  </code></pre>
  
  <p>The error:</p>
  
  <pre><code>SequelizeDatabaseError: column "undefined" does not exist
  </code></pre>
  
  <p>I assume this error is occurring because sqzl doesn't understand the proper way to identify rows in the join table, because I've failed to establish the necessary associations. And frankly, I'm not confident I've done this correctly; I'm new to ORMs, but I was expecting I'd need to specify 4 assocations:</p>
  
  <ol>
  <li><code>Driver</code> -> <code>DriverCar</code></li>
  <li><code>DriverCar</code> -> <code>Car</code></li>
  <li><code>Car</code> -> <code>DriverCar</code></li>
  <li><code>DriverCar</code> -> <code>Driver</code></li>
  </ol>
  
  <hr>
  
  <p>To recap: I have 2 first-class entities, joined in a many-to-many relationship. I'm trying to add data to the relationship, have discovered that the ORM requires defining those associations differently, and am having trouble articulating the new associations.</p>