title: Sequelize Migrations: Adding a foreign key constraint to a column on same table
body: <p>So I'm trying to create a table with foreign key constraints to itself in migrations file.</p>
  
  <p>I tried what I could following the <a href="http://docs.sequelizejs.com/class/lib/query-interface.js~QueryInterface.html#instance-method-addConstraint" rel="noreferrer">sequelize docs</a> and down below is the code I've tried, and I've also tried to move the foreign key references up to where the attributes were defined but it does not work there as well. Is there a way to do what I want to do here?</p>
  
  <pre><code>module.exports = {
    up: (queryInterface, Sequelize) =&gt; {
      return queryInterface.createTable('comments', {
        id: {
          allowNull: false,
          autoIncrement: true,
          primaryKey: true,
          type: Sequelize.INTEGER
        },
        root_id: {
          defaultValue: null,
          type: Sequelize.INTEGER
        },
        parent_id: {
          defaultValue: null,
          type: Sequelize.INTEGER
        },
      }).then(() =&gt; queryInterface.addConstraint(
        'comments',
        ['root_id'],
        {
          type: 'foreign key',
          name: 'root_id_fk',
          references: {
            table: 'comments',
            field: 'root_id'
          },
          onDelete: 'cascade',
          onUpdate: 'cascade'
        }
      )).then(() =&gt; queryInterface.addConstraint(
        'comments',
        ['parent_id'],
        {
          type: 'foreign key',
          name: 'parent_id_fk',
          references: {
            table: 'comments',
            field: 'parent_id'
          },
          onDelete: 'cascade',
          onUpdate: 'cascade'
        }
      ))
    },
  </code></pre>