title: Foreign key constraint fails when adding a foreign key to an existing table in Laravel
body: <p>I have a table order_lines, which has an order_id that belongs to orders.
  Now I forgot to add the foreign key to the initial migration, now the website is up and running and there's life data in the database.</p>
  
  <p>The foreign key I would like to add would delete an order line when an order is deleted.</p>
  
  <p>I created the following migration <code>add_order_lines_order_id_foreign_to_order_lines_table</code></p>
  
  <pre class="lang-php prettyprint-override"><code>class AddOrderLinesOrderIdForeignToOrderLinesTable extends Migration
  {
      /**
       * Run the migrations.
       *
       * @return void
       */
      public function up()
      {
          Schema::table('order_lines', function (Blueprint $table) {
              $table-&gt;foreign('order_id', 'order_lines_order_id_foreign')-&gt;references('id')-&gt;on('orders')
                  -&gt;onDelete('cascade');
          });
      }
  
      /**
       * Reverse the migrations.
       *
       * @return void
       */
      public function down()
      {
          Schema::table('order_lines', function (Blueprint $table) {
              $table-&gt;dropForeign('order_lines_order_id_foreign');
          });
      }
  }
  </code></pre>
  
  <p>The only things this is doing is it's (trying to) add a foreign key constraint, so that when future orders get deleted, the order_lines get deleted with it.</p>
  
  <p>This is the error I'm receiving when I try to run the migration</p>
  
  <blockquote>
    <p>SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (bud001_miguel.#sql-5e1_109923, CONSTRAINT order_lines_order_id_foreign FOREIGN KEY (order_id) REFERENCES orders (id) ON DELETE CASCADE) (SQL: alter table order_lines add constraint order_lines_order_id_foreign foreign key (order_id) references orders (id) on delete cascade)</p>
  </blockquote>
  
  <p>This is the original <code>orders</code> table definition</p>
  
  <pre><code>class CreateOrdersTable extends Migration
  {
      /**
       * Run the migrations.
       *
       * @return void
       */
      public function up()
      {
          Schema::create('orders', function (Blueprint $table) {
              $table-&gt;bigIncrements('id');
              $table-&gt;unsignedBigInteger('user_id')-&gt;nullable();
              $table-&gt;unsignedBigInteger('shipping_address_id')-&gt;nullable();
              $table-&gt;unsignedBigInteger('billing_address_id')-&gt;nullable();
              $table-&gt;text('shipping_address_data')-&gt;nullable();
              $table-&gt;text('billing_address_data')-&gt;nullable();
              $table-&gt;timestamps();
          });
  
          Schema::table('orders', function (Blueprint $table) {
              $table-&gt;foreign('user_id')-&gt;references('id')-&gt;on('users')-&gt;onDelete('set null');
              $table-&gt;foreign('shipping_address_id')-&gt;references('id')-&gt;on('addresses')-&gt;onDelete('set null');
              $table-&gt;foreign('billing_address_id')-&gt;references('id')-&gt;on('addresses')-&gt;onDelete('set null');
          });
      }
  
      /**
       * Reverse the migrations.
       *
       * @return void
       */
      public function down()
      {
          Schema::dropIfExists('orders');
      }
  }
  </code></pre>
  
  <p>This is the original <code>order_lines</code> table definition</p>
  
  <pre><code>class CreateOrderLinesTable extends Migration
  {
      /**
       * Run the migrations.
       *
       * @return void
       */
      public function up()
      {
          Schema::create('order_lines', function (Blueprint $table) {
              $table-&gt;bigIncrements('id');
              $table-&gt;unsignedBigInteger('order_id');
              $table-&gt;unsignedBigInteger('animal_id');
              $table-&gt;unsignedBigInteger('product_plan_id');
              $table-&gt;unsignedInteger('price');
              $table-&gt;unsignedInteger('daily_price');
              $table-&gt;text('animal_data')-&gt;nullable();
              $table-&gt;text('plan_data')-&gt;nullable();
              $table-&gt;timestamps();
          });
      }
  
      /**
       * Reverse the migrations.
       *
       * @return void
       */
      public function down()
      {
          Schema::dropIfExists('order_lines');
      }
  }
  </code></pre>
  
  <h2>What I've tried</h2>
  
  <ul>
  <li>Disable foreign key checks by adding the following statement to the migration
  <code>DB::statement('SET FOREIGN_KEY_CHECKS=0;');</code></li>
  </ul>
  
  <p>But the error persists.</p>
  
  <p><a href="https://i.stack.imgur.com/Ynvts.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Ynvts.png" alt="enter image description here"></a></p>