title: Laravel Model relations with foreign key constraints
body: <p>I'm trying to get foreign key constraints to work in Laravel. But I can't seem to save my models anymore. </p>
  
  <p>My database structure is setup as follows (I've removed unnecessary code):</p>
  
  <pre><code>Schema::create($this-&gt;tableName, function(Blueprint $table)
  {
      $table-&gt;increments('id');
  
      $table-&gt;string('username')-&gt;unique();
      $table-&gt;string('password', 64);
  
      $table-&gt;integer('address_id')-&gt;unsigned();
  });
  
  Schema::create($this-&gt;tableName, function(Blueprint $table)
  {
      $table-&gt;increments('id');
  
      $table-&gt;integer('user_id')-&gt;unsigned();
  
      $table-&gt;string('name');
      $table-&gt;string('surname');
  
      $table-&gt;string('street');
      $table-&gt;string('number');
      $table-&gt;string('town');
      $table-&gt;string('country');
  });
  </code></pre>
  
  <p>So the <code>Address</code> is in a separate table and an <code>Address</code> belongs to a <code>User</code>. The <code>User</code> has an <code>Address</code>.</p>
  
  <p>Next I've created the corresponding models:</p>
  
  <pre><code>Class User extends Model
  {
      public function address()
      {
          return $this-&gt;hasOne('Address');
      }
  }
  
  Class Address extends Model
  {
      public function user()
      {
          return $this-&gt;belongsTo('User');
      }
  }
  </code></pre>
  
  <p>Now when I try to create a <code>User</code> with an <code>Address</code> and save it I get constraint errors. I've tried it both ways (saving the <code>User</code> first and saving the <code>Address</code> first) but both give an error.</p>
  
  <p>The first idea I had was this:</p>
  
  <pre><code>// Create a new user
  $user = new User($data);
  $address = new Address($addressData);
  
  $address-&gt;user()-&gt;associate($user);
  $user-&gt;push(); // Error here
  </code></pre>
  
  <p>But this gives the error:</p>
  
  <pre><code>SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`eet`.`users`, CONSTRAINT `users_address_id_foreign` FOREIGN KEY (`address_id`) REFERENCES `addresses` (`id`)) (SQL: insert into `users` (`username`, `email`, `password`, `type`, `phone`, `cellphone`, `newsletter_week`, `ip`, `updated_at`, `created_at`) values (adsadsad, validemail@gmail.comx, passWO23dw00, customer, 123123213, 1234567890, 1, ::1, 2014-10-05 19:56:03, 2014-10-05 19:56:03))
  </code></pre>
  
  <p>So I tried saving the <code>Address</code> first, and then the <code>User</code> like:</p>
  
  <pre><code>$user = new User($data);
  
  $address = new Address($addressData);
  $address-&gt;save(); // Error here
  
  $address-&gt;user()-&gt;associate($user);
  $user-&gt;push();
  </code></pre>
  
  <p>But then the following error is generated:</p>
  
  <pre><code>SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'user_id' cannot be null (SQL: insert into `addresses` (`name`, `surname`, `street`, `number`, `town`, `country`, `user_id`) values (Foo, Bar, FooStreet, 200, Townsville, nl, ))
  </code></pre>
  
  <p>So what is a good way to do this? I could make the field <code>address_id</code> for the <code>User</code> nullable, but is that a good practise? I want a <code>User</code> to always have an <code>Address</code>. </p>