title: Laravel - Unable to Migrate table with both 1:N and 1:1 Relationships
body: <p>I have the following Migration tables:</p>
  
  <pre><code>class CreateQuizzesTable extends Migration
  {
      public function up()
      {
          Schema::create('quizzes', function (Blueprint $table) {
              $table-&gt;increments('id');
              $table-&gt;integer('customer_id')-&gt;unsigned()-&gt;nullable();
              $table-&gt;string('name');
              $table-&gt;foreign('customer_id')-&gt;references('id')-&gt;on('customers')-&gt;onDelete('set null');
          });
      }
  
      public function down()
      {
          Schema::dropIfExists('quizzes');
      }
  }
  </code></pre>
  
  <p>And </p>
  
  <pre><code>class CreateCustomersTable extends Migration
  {
      public function up()
      {
          Schema::create('customers', function (Blueprint $table) {
              $table-&gt;increments('id');
              $table-&gt;integer('quiz_id')-&gt;unsigned()-&gt;nullable();
              $table-&gt;string('name')-&gt;unique();
              $table-&gt;string('password');
              $table-&gt;foreign('quiz_id')-&gt;references('id')-&gt;on('quizzes')-&gt;onDelete('set null');
          });
      }
  
      public function down()
      {
          Schema::dropIfExists('customers');
      }
  }
  </code></pre>
  
  <p>A customer can create (and thus has) multiple quizzes (1:N). The reason why the <strong>customers</strong> table has a <strong>quiz_id</strong> field is that, of those he has created, only one can be active at a time (1:1).</p>
  
  <p>The problem is that it crashes when I try to migrate, since both tables reference eachother, notifying me that the other table doesn't exist yet.</p>
  
  <p>If I set <strong>CreateQuizzesTable</strong> to execute first:</p>
  
  <blockquote>
    <p>SQLSTATE[42P01]: Undefined table: 7 ERROR: relation does not exist «customers» </p>
  </blockquote>
  
  <p>If I set <strong>CreateCustomersTable</strong> to execute first:</p>
  
  <blockquote>
    <p>SQLSTATE[42P01]: Undefined table: 7 ERROR: relation does not exist «quizzes»</p>
  </blockquote>
  
  <p>I insist, this is not an N:M relationship, I would like to avoid intermediate tables to manage this.</p>
  
  <p>UPDATE: after checking out @JasperStaats answer, I still have issues with the seeding of the tables. Since one of the two table seeders has to be executed first, it expects the foreign key value of the other table to already exist in the DB (e.g.: if I execute QuizzesTableSeeder first, it will be unable to insert the value 'customer_id' => 1 since the customers table is still empty. For now, I leave that field value null and then go to the DB and replace it with appropiate value, but this is not ideal.</p>
  
  <p>Seed Execution Order File:</p>
  
  <pre><code>use Illuminate\Database\Seeder;
  
  class DatabaseSeeder extends Seeder
  {
      public function run()
      {
          $this-&gt;call(QuizzesTableSeeder::class);
          $this-&gt;call(CustomersTableSeeder::class);
      }
  }
  </code></pre>
  
  <p>Customer seeder file:</p>
  
  <pre><code>use Illuminate\Database\Seeder;
  
  class CustomersTableSeeder extends Seeder
  {
      public function run()
      {
          \DB::table('customers')-&gt;delete();
  
          \DB::table('customers')-&gt;insert(array(
              0 =&gt;
              array(
                  'id' =&gt; 1,
                  'quiz_id' =&gt; 1,
                  'name' =&gt; 'user1',
                  'password' =&gt; 'pass1'
              )
          ));
      }
  }
  </code></pre>
  
  <p>Quiz seeder file:</p>
  
  <pre><code>use Illuminate\Database\Seeder;
  
  class QuizzesTableSeeder extends Seeder
  {
      public function run()
      {
          \DB::table('quizzes')-&gt;delete();
  
          \DB::table('quizzes')-&gt;insert(array(
              0 =&gt;
              array(
                  'id' =&gt; 1,
                  'customer_id' =&gt; 1,
                  'name' =&gt; 'quizz1',
              )
          ));
      }
  }
  </code></pre>