title: Getting "Integrity constraint violation: 1452" in Laravel
body: <p>I am using two pivot tables to store two different relations of a table. One relation works fine on insert. but the next one throws the following error. I don't know what's causing this.</p>
  
  <blockquote>
    <p>SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (<code>sdesk</code>.<code>approvers</code>, CONSTRAINT <code>approvers_user_id_foreign</code> FOREIGN KEY (<code>user_id</code>) REFERENCES <code>users</code> (<code>id</code>)) (SQL: insert into <code>approvers</code> (<code>user_id</code>, <code>request_id</code>, <code>updated_at</code>, <code>created_at</code>) values (2, 6, 2014-04-29 10:54:37, 2014-04-29 10:54:37)) </p>
  </blockquote>
  
  <p>Here's my code:</p>
  
  <p><strong>UrequestsController.php</strong> </p>
  
  <pre><code> public function postCreate() { 
      $request = new Urequest;
      $request-&gt;vms = Input::get('vms');
      $request-&gt;location = Input::get('location');
      $request-&gt;descr = Input::get('descr');
      $request-&gt;status = Input::get('status');
      //$request-&gt;role_group = Input::get('team');
      $request-&gt;save();
  
      $ruser = new Ruser;
      $ruser-&gt;user_id = Input::get('userid');
      $ruser-&gt;urequest()-&gt;associate($request);
      $ruser-&gt;save();
  
      $approver = new Approver;
      $approver-&gt;user_id = Input::get('aid');
      $approver-&gt;urequest()-&gt;associate($request);
      $approver-&gt;save();
  
  
      return Redirect::to('users/dashboard')-&gt;with('message', 'Saved!');
  }
  </code></pre>
  
  <p><strong>Approver.php</strong></p>
  
  <pre><code> class Approver extends Eloquent {
  
  
  protected $fillable = array('request_id', 'user_id');
  
  
  public function urequest() {
      return $this-&gt;belongsTo('Urequest','request_id'); 
  }
  
   }
  </code></pre>
  
  <p><strong>Ruser.php</strong></p>
  
  <pre><code> class Ruser extends Eloquent {
  
  
  protected $fillable = array('request_id', 'user_id');
  
  
  public function urequest() {
      return $this-&gt;belongsTo('Urequest','request_id');
  }
  
   }
  </code></pre>
  
  <p><strong>urequest.php</strong></p>
  
  <pre><code> class Urequest extends Eloquent {
  
  protected $table = 'requests';
  protected $fillable = array('vms', 'location', 'descr', 'status');
  
  public function ruser() {
      return $this-&gt;hasOne('Ruser'); 
  }
  
  public function approver() {
      return $this-&gt;hasOne('Approver'); 
  }
  
   } 
  </code></pre>
  
  <p><strong>Schema</strong></p>
  
  <pre><code> Schema::create('requests', function($table)
  {
              $table-&gt;increments('id');
              $table-&gt;string('vms', 20);
              $table-&gt;string('location', 20);
                      $table-&gt;string('descr', 255);
              $table-&gt;string('status', 20);
              $table-&gt;timestamps();
          });
  
   Schema::create('rusers', function($table)
      {
          $table-&gt;increments('id');
          $table-&gt;integer('request_id')-&gt;unsigned();
          $table-&gt;foreign('request_id')-&gt;references('id')-&gt;on('requests');
          $table-&gt;integer('user_id')-&gt;unsigned();
          $table-&gt;foreign('user_id')-&gt;references('id')-&gt;on('users');
          $table-&gt;timestamps();
      });
  
    Schema::create('approvers', function($table)
      {
          $table-&gt;increments('id');
          $table-&gt;integer('request_id')-&gt;unsigned();
          $table-&gt;foreign('request_id')-&gt;references('id')-&gt;on('requests');
          $table-&gt;integer('user_id')-&gt;unsigned();
          $table-&gt;foreign('user_id')-&gt;references('id')-&gt;on('users');
          $table-&gt;timestamps();
      });
  </code></pre>