title: How do I turn references in Sequelize Auto to Associations in Sequelize?
body: <p>I use <a href="https://github.com/sequelize/sequelize-auto" rel="nofollow noreferrer">Sequelize Auto</a> to generate my database definitions. Inside the definitions it defines which columns are foreign keys with the "references" property. However, </p>
  
  <pre><code>$ cat models/account.js
  /* jshint indent: 2 */
  
  module.exports = function(sequelize, DataTypes) {
    return sequelize.define('account', {
      id: {
        type: DataTypes.BIGINT,
        allowNull: false,
        primaryKey: true,
        autoIncrement: true
      },
      parent_account_id: {
        type: DataTypes.BIGINT,
        allowNull: false,
        references: {
          model: 'account',
          key: 'id'
        }
      },
      master_account_id: {
        type: DataTypes.BIGINT,
        allowNull: false,
        references: {
          model: 'account',
          key: 'id'
        }
      },
      name: {
        type: DataTypes.STRING,
        allowNull: false
      },
      description: {
        type: DataTypes.STRING,
        allowNull: false,
        defaultValue: ''
      },
      enabled: {
        type: DataTypes.INTEGER(1),
        allowNull: false,
        defaultValue: '1'
      },
      account_type_id: {
        type: DataTypes.INTEGER(11),
        allowNull: false
      },
      billing_account_id: {
        type: DataTypes.BIGINT,
        allowNull: false,
        references: {
          model: 'account',
          key: 'id'
        }
      },
      contact_name: {
        type: DataTypes.STRING,
        allowNull: false,
        defaultValue: ''
      },
      contact_email: {
        type: DataTypes.STRING,
        allowNull: false,
        defaultValue: ''
      },
      data_feeds_path: {
        type: DataTypes.STRING,
        allowNull: false,
        defaultValue: ''
      },
      requests: {
        type: DataTypes.BIGINT,
        allowNull: false,
        defaultValue: '0'
      },
      true_impressions: {
        type: DataTypes.BIGINT,
        allowNull: false,
        defaultValue: '0'
      },
      clicks: {
        type: DataTypes.BIGINT,
        allowNull: false,
        defaultValue: '0'
      },
      actions: {
        type: DataTypes.BIGINT,
        allowNull: false,
        defaultValue: '0'
      },
      conversions: {
        type: DataTypes.BIGINT,
        allowNull: false,
        defaultValue: '0'
      },
      status_id: {
        type: DataTypes.BIGINT,
        allowNull: false,
        defaultValue: '1',
        references: {
          model: 'status',
          key: 'id'
        }
      },
      status_modified_date: {
        type: DataTypes.DATE,
        allowNull: false,
        defaultValue: '0001-01-01 00:00:00'
      },
      date_created: {
        type: DataTypes.DATE,
        allowNull: false,
        defaultValue: '0001-01-01 00:00:00'
      },
      date_created_user_id: {
        type: DataTypes.BIGINT,
        allowNull: false,
        defaultValue: '1',
        references: {
          model: 'user',
          key: 'id'
        }
      },
      date_modified: {
        type: DataTypes.DATE,
        allowNull: false,
        defaultValue: '0001-01-01 00:00:00'
      },
      date_modified_user_id: {
        type: DataTypes.BIGINT,
        allowNull: false,
        defaultValue: '1',
        references: {
          model: 'user',
          key: 'id'
        }
      },
      date_deleted: {
        type: DataTypes.DATE,
        allowNull: true
      },
      date_deleted_user_id: {
        type: DataTypes.BIGINT,
        allowNull: true,
        references: {
          model: 'user',
          key: 'id'
        }
      }
    }, {
      tableName: 'account'
    });
  };
  </code></pre>
  
  <p>If I try to use joins in my Sequelize query, I receive:</p>
  
  <pre><code>Unhandled rejection Error: X is not associated to Y!
      at Model.validateIncludedElement (/Library/WebServer/adstudio/node_modules/sequelize/lib/model.js:558:11)
      at /Library/WebServer/adstudio/node_modules/sequelize/lib/model.js:440:29
      at Array.map (native)
      at Model.validateIncludedElements (/Library/WebServer/adstudio/node_modules/sequelize/lib/model.js:436:37)
      at Model.aggregate (/Library/WebServer/adstudio/node_modules/sequelize/lib/model.js:1558:30)
      at Model.&lt;anonymous&gt; (/Library/WebServer/adstudio/node_modules/sequelize/lib/model.js:1624:17)
      at runCallback (timers.js:666:20)
      at tryOnImmediate (timers.js:639:5)
  </code></pre>
  
  <p>Here's the code that I've tried to use to add associations after importing the model definitions from my Sequelize Auto files:</p>
  
  <pre><code>        console.log("Length of models: " + Object.keys(instance.sequelize.models).length)
          for (var objectName in instance.sequelize.models) {
              var model = instance.sequelize.models[objectName];
              var columns = instance.sequelize.modelManager.getModel(model.name).attributes;
              console.log('Processing foreign keys for ' + model.name);
              for (var columnName in columns) {
                  var column = columns[columnName];
                  if (column.references) {
                      //if (column.references.key == column.references.model + '_id') {
                      if (model.name == column.references.model) {
                          console.log("  " + model.name + ' has one ' + column.fieldName.replace("_id", ""));
                          model.belongsTo(model, { as: column.fieldName.replace("_id", ""), foreignKey: column.fieldName });
                      } else {
                          //console.log(model.name + ' has one ' + column.references.model);
                          //console.log(column.references.model + ' has many ' + model.name);
  
                          // Will add foreignKey to the right-value table
                          //console.log("  " + model.name + " belongs to " + column.references.model + " on foreign key " + column.references.key);
                          //model.belongsTo(instance.sequelize.models[column.references.model], { foreignKey: column.references.key });
  
                          console.log("  " + column.references.model + " belongs to " + model.name + " on foreign key " + column.fieldName );
                          instance.sequelize.models[column.references.model].belongsTo(model, { foreignKey: column.fieldName });
  
                          console.log(model);
  
                          //model.hasOne(instance.sequelize.models[column.references.model]);
                          //instance.sequelize.models[column.references.model].hasMany(model);
                      }
                  }
              }
          }
  </code></pre>
  
  <p>I've tried just about every combination and the associations just don't define correctly, like saying that the account.id column is a foreign key for the account_audit_log table. I've tried just about every variation I can and can't see where it's possible.</p>