title: Cannot add foreign key constraint [Laravel 5.6]
body: <p><strong>tl;dr. Solution:</strong></p>
  
  <p>Thanks to <a href="https://stackoverflow.com/a/50900204/4777323">Jonas</a>.</p>
  
  <p>the problem was that the tables I was referring as foreign, were not InnoDB. </p>
  
  <p>I added raw SQL statements in alter migrations and then added the foreign keys:</p>
  
  <p><code>DB::statement("ALTER TABLE table ENGINE='InnoDB';");</code></p>
  
  <p><strong>Original question</strong></p>
  
  <p>First, before the Stackoverflow police bust me, I know this question is probably 83% of the database of this website. But me is special (Kidding, I know I'm not). But I've tried most of the common stuff and nothing seems to work. So probably I'm overseeing something.</p>
  
  <p><strong>Error</strong></p>
  
  <p>General error: 1215 Cannot add foreign key constraint (SQL: alter table <code>applications</code> add constraint <code>applications_user_id_foreign</code> foreign key (<code>user_id</code>) references <code>users</code> (<code>id</code>) on delete cascade)</p>
  
  <p><strong>This is my migration:</strong></p>
  
  <pre><code>public function up()
  {
      Schema::create("applications", function(Blueprint $table) {
          $table-&gt;engine = "InnoDB";
          $table-&gt;increments('id');
          $table-&gt;timestamps();
      });
  
      Schema::table('applications', function($table) {
  
          $table-&gt;integer('user_id')-&gt;unsigned()-&gt;index();
          $table-&gt;foreign('user_id')-&gt;references('id')-&gt;on('users')-&gt;onDelete('cascade');
          $table-&gt;integer('job_request_id')-&gt;unsigned()-&gt;index();
          $table-&gt;foreign('job_request_id')-&gt;references('id')-&gt;on('job_requests')-&gt;onDelete('cascade');
          $table-&gt;integer('status')-&gt;default(0);
      });
  }
  </code></pre>
  
  <p>What I've already tried:</p>
  
  <p>1.</p>
  
  <pre><code>public function up()
  {
      Schema::create("applications", function(Blueprint $table) {
          $table-&gt;increments('id');
          $table-&gt;timestamps();
  
          $table-&gt;integer('user_id')-&gt;unsigned();
          $table-&gt;foreign('user_id')-&gt;references('id')-&gt;on('users')-&gt;onDelete('cascade');
          $table-&gt;integer('job_request_id')-&gt;unsigned();
          $table-&gt;foreign('job_request_id')-&gt;references('id')-&gt;on('job_requests')-&gt;onDelete('cascade');
          $table-&gt;integer('status')-&gt;default(0);
      });
  }
  </code></pre>
  
  <p>2.</p>
  
  <pre><code>public function up()
  {
      Schema::create("applications", function(Blueprint $table) {
          $table-&gt;engine = "InnoDB";
          $table-&gt;increments('id');
          $table-&gt;timestamps();
  
          $table-&gt;integer('user_id')-&gt;unsigned();
          $table-&gt;foreign('user_id')-&gt;references('id')-&gt;on('users')-&gt;onDelete('cascade');
          $table-&gt;integer('job_request_id')-&gt;unsigned();
          $table-&gt;foreign('job_request_id')-&gt;references('id')-&gt;on('job_requests')-&gt;onDelete('cascade');
          $table-&gt;integer('status')-&gt;default(0);
      });
  }
  </code></pre>
  
  <ol start="3">
  <li>Splitting the migration into two files (A create and an alter). Even adding each reference one by one.</li>
  </ol>
  
  <p>4.- using DB::statement('SET FOREIGN_KEY_CHECKS=0;'); and =1 at the beginning an the end og the migration.</p>
  
  <p>5.- removing the unsigned() and the index().</p>
  
  <p>Might mean something: </p>
  
  <p>1.- When I rollback the migration, it doesn't delete the table. So if I rollback and migrate, would give me a "already exists error". </p>
  
  <p>2.- I already have migrations which reference the same items, i.e:</p>
  
  <pre><code>Schema::create('job_requests', function (Blueprint $table) {
          ...
          $table-&gt;integer('user_id')-&gt;unsigned()-&gt;nullable();
          $table-&gt;foreign('user_id')-&gt;references('id')-&gt;on('users');
          ...
      });
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>For the drop methods I've tried:</p>
  
  <ol>
  <li><p>For the create migrations</p>
  
  <p>public function down()
  {
      Schema::drop('applications');
  }</p>
  
  <p>public function down()
  {
      Schema::dropIfExists('applications');
  }</p></li>
  </ol>
  
  <p>2.- For the alter migrations</p>
  
  <pre><code>public function down()
  {
      Schema::table('applications', function (Blueprint $table) {
          $table-&gt;dropForeign(['user_id']);
          $table-&gt;dropColumn('user_id');
          $table-&gt;dropForeign(['job_request_id']);
          $table-&gt;dropColumn('job_request_id');
      });
  }
  </code></pre>
  
  <p><strong>UPDATE 2:</strong></p>
  
  <pre><code>public function up()
  {
      Schema::create('users', function (Blueprint $table) {
          $table-&gt;increments('id');
          $table-&gt;string('first_name');
          $table-&gt;string('last_name')-&gt;nullable();
          $table-&gt;string('password');
          $table-&gt;rememberToken();
          $table-&gt;timestamps();
      });
  }
  
  /**
   * Reverse the migrations.
   *
   * @return void
   */
  public function down()
  {
      Schema::dropIfExists('users');
  }
  
  public function up()
  {
      Schema::create('job_requests', function (Blueprint $table) {
          $table-&gt;increments('id');
          $table-&gt;integer('status')-&gt;default(0);
          $table-&gt;integer('user_id')-&gt;unsigned()-&gt;nullable();
          $table-&gt;foreign('user_id')-&gt;references('id')-&gt;on('users');
          $table-&gt;timestamps();
      });
  }
  
  /**
   * Reverse the migrations.
   *
   * @return void
   */
  public function down()
  {
      Schema::dropIfExists('job_requests');
  }
  </code></pre>
  
  <p>I added three more alter migration:</p>
  
  <pre><code>public function up()
  {
      Schema::table('users', function (Blueprint $table) {
          $table-&gt;engine = "InnoDB";
      });
  }
  
  ///////////////////////////
  
  public function up()
  {
      Schema::table('job_requests', function (Blueprint $table) {
          $table-&gt;engine = "InnoDB";
      });
  }
  
  ///////////////////////////
  
  public function up()
  {
      Schema::table('applications', function (Blueprint $table) {
          $table-&gt;foreign('user_id')-&gt;references('id')-&gt;on('users')-&gt;onDelete('cascade');
          $table-&gt;foreign('job_request_id')-&gt;references('id')-&gt;on('job_requests')-&gt;onDelete('cascade');
      });
  }
  
  /**
   * Reverse the migrations.
   *
   * @return void
   */
  public function down()
  {
      Schema::table('applications', function (Blueprint $table) {
          $table-&gt;dropForeign(['user_id']);
          $table-&gt;dropForeign(['job_request_id']);
      });
  }
  </code></pre>
  
  <p>Without luck yet.</p>