title: General error: 1215 Cannot add foreign key constraint in laravel
body: <p>Doing the migrations I got this error:
   SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table <code>books</code> add constraint <code>books_writer_id_foreign</code> foreign key (<code>writer_id</code>) references <code>writers</code> (<code>id</code>))
  I've tried a lot of things but noone looks to work.</p>
  
  <p>2018_02_18_3165165_create_books_table.php</p>
  
  <pre><code>&lt;?php
  
  use Illuminate\Support\Facades\Schema;
  use Illuminate\Database\Schema\Blueprint;
  use Illuminate\Database\Migrations\Migration;
  
  class CreateBooksTable extends Migration
  {
      /**
       * Run the migrations.
       *
       * @return void
       */
      public function up()
      {
          Schema::create('books', function (Blueprint $table) {
              $table-&gt;engine = 'InnoDB';
  
              $table-&gt;increments('id');
  
  
              $table-&gt;string('name');
              $table-&gt;text('description');
  
              $table-&gt;integer('numPages');
              $table-&gt;enum('language', ['spanish', 'english']);
  
              $table-&gt;date('wrote_date')-&gt;nullable();
  
  
              $table-&gt;timestamp('created_at')-&gt;useCurrent();
              $table-&gt;timestamp('updated_at')-&gt;default(DB::raw('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'));
          });
          Schema::table('books', function (Blueprint $table) {
              $table-&gt;integer('writer_id')-&gt;unsigned();
              $table-&gt;foreign('writer_id')-&gt;references('id')-&gt;on('writers');
          });
      }
  
      /**
       * Reverse the migrations.
       *
       * @return void
       */
      public function down()
      {
          Schema::dropIfExists('books');
      }
  }
  </code></pre>
  
  <p>2018_02_18_192915_create_writers_table
      
  
  <pre><code>use Illuminate\Support\Facades\Schema;
  use Illuminate\Database\Schema\Blueprint;
  use Illuminate\Database\Migrations\Migration;
  
  class CreateWritersTable extends Migration
  {
      /**
       * Run the migrations.
       *
       * @return void
       */
      public function up()
      {
          Schema::create('writers', function (Blueprint $table) {
              $table-&gt;increments('id');
              $table-&gt;string('name');
              $table-&gt;text('description');
  
              $table-&gt;string('nationality');
              $table-&gt;date('year_date')-&gt;nullable();
              $table-&gt;date('dead_date')-&gt;nullable();
  
              $table-&gt;timestamp('created_at')-&gt;useCurrent();
              $table-&gt;timestamp('updated_at')-&gt;default(DB::raw('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'));
          });
      }
  
      /**
       * Reverse the migrations.
       *
       * @return void
       */
      public function down()
      {
          Schema::dropIfExists('writers');
      }
  }
  </code></pre>
  
  <p>Edit: <strong>The error was because the first migration was the books, and then the writers causing that the error.</strong></p>