title: How to define sequelize associations in typescript?
body: <p>I have a Product table and an AccountingPeriod table, with a <em>belongsTo</em> relationship from Product.manufacturingPeriodId to AccountingPeriod.id.</p>
  
  <p>The code below compiles but blows up with <strong>"Naming collision between attribute 'manufacturingPeriod' and association 'manufacturingPeriod' on model Product. To remedy this, change either foreignKey or as in your association definition"</strong> at run time.</p>
  
  <p>If I change <code>as</code> in the association code at the very bottom as instructed, I also blow up, but this time with <strong>"AccountingPeriod is associated to Product using an alias. You've included an alias (accountingPeriod), but it does not match the alias defined in your association".</strong> That second error message is especially puzzling, since I don't specify an alias named <em>accountingPeriod</em>.</p>
  
  <p>Doh! Seems like a Catch-22 to me. </p>
  
  <p>Of course, I can remove <code>ProductAttributes.manufacturingPeriod</code>, put back <code>manufacturingPeriodId: number;</code> and rename <code>manufacturingPeriod</code> back to <code>manufacturingPeriodId</code> in the options object in the call to sequelize.define(). That compiles and runs just fine, but then I can't code something like <code>myproduct.manufacturingPeriod.startDate</code> in typescript.</p>
  
  <p>I've tried various other approaches. All have failed, so I'm raising the white flag of surrender. Can anyone help me out? I'm experienced with sequelize but relatively new to typescript and I'm just not seein' it. </p>
  
  <pre><code>import * as Sequelize from 'sequelize';
  import {ObjectRelationalManager as orm} from '../index';
  import {AccountingPeriodInstance} from './accounting-period';
  
  export interface ProductAttributes {
      id?: number;
      name: string;
      manufacturingPeriod: AccountingPeriodInstance;
  }
  
  export interface ProductInstance extends Sequelize.Instance&lt;ProductAttributes&gt;, ProductAttributes {}
  
  export default (
      sequelize: Sequelize.Sequelize,
      dataTypes: Sequelize.DataTypes
  ): Sequelize.Model&lt;ProductInstance, ProductAttributes&gt; =&gt; {
      return sequelize.define&lt;ProductInstance, ProductAttributes&gt;(
          'Product',
          {
              id: {
                  type: dataTypes.INTEGER,
                  field: 'id',
                  allowNull: false,
                  primaryKey: true,
                  autoIncrement: true
              },
              name: {
                  type: dataTypes.STRING(20),
                  field: 'name',
                  allowNull: false
              },
              manufacturingPeriod: {
                  type: dataTypes.INTEGER,
                  field: 'manufacturingPeriodId',
                  allowNull: false,
                  references: {
                      model: 'AccountingPeriod',
                      key: 'id'
                  },
                  onDelete: 'NO ACTION',
                  onUpdate: 'NO ACTION'
              }
          },
          {
              tableName: 'Product'
          }
      );
  };
  
  export function createAssociations(): void {
      orm.Product.belongsTo(orm.AccountingPeriod, {
          as: 'manufacturingPeriod',
          // foreignKey: 'manufacturingPeriodId',
          targetKey: 'id',
          onDelete: 'NO ACTION',
          onUpdate: 'NO ACTION'
      });
  }
  </code></pre>