title: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails - Laravel
body: <p>I know that this question has already been asked, but I still can't find what I'm doing wrong.</p>
  
  <p>I'm using the framework Laravel.</p>
  
  <p>I have 2 tables (Users and Locations). When I want to create a User, I get the error message:</p>
  
  <blockquote>
    <p>SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or
    update a child row: a foreign key constraint fails
    (<code>festival_aid</code>.<code>users</code>, CONSTRAINT <code>fk_users_locations1</code> FOREIGN KEY
    (<code>location_id</code>) REFERENCES <code>locations</code> (<code>location_id</code>) ON DELETE
    CASCADE ON UPDATE NO ACTION) (SQL: insert into <code>users</code> (<code>user_id</code>,
    <code>user_email</code>, <code>location_id</code>) values (?, ?, ?)) (Bindings: array ( 0 =>
    '1', 1 => 'test@hotmail.com', 2 => '1', ))</p>
  </blockquote>
  
  <p>Table Users</p>
  
  <pre><code>CREATE TABLE IF NOT EXISTS `festival_aid`.`users` (
    `user_id` BIGINT NOT NULL AUTO_INCREMENT,
    `user_email` VARCHAR(45) NOT NULL,
    `user_created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `user_modified` TIMESTAMP NULL,
    `user_deleted` TIMESTAMP NULL,
    `user_lastlogin` TIMESTAMP NULL,
    `user_locked` TIMESTAMP NULL,
    `location_id` BIGINT NOT NULL,
    PRIMARY KEY (`user_id`),
    UNIQUE INDEX `user_email_UNIQUE` (`user_email` ASC),
    CONSTRAINT `fk_users_locations1`
      FOREIGN KEY (`location_id`)
      REFERENCES `festival_aid`.`locations` (`location_id`)
      ON DELETE CASCADE
      ON UPDATE NO ACTION,
  ENGINE = InnoDB;
  </code></pre>
  
  <p>Table Locations</p>
  
  <pre><code>DROP TABLE IF EXISTS `festival_aid`.`locations` ;
  
  CREATE TABLE IF NOT EXISTS `festival_aid`.`locations` (
    `location_id` BIGINT NOT NULL AUTO_INCREMENT,
    `location_latitude` FLOAT NOT NULL,
    `location_longitude` FLOAT NOT NULL,
    `location_desc` VARCHAR(255) NULL,
    `location_type` VARCHAR(45) NULL,
    PRIMARY KEY (`location_id`))
  ENGINE = InnoDB;
  </code></pre>
  
  <p>Migration User</p>
  
  <pre><code>public function up()
      {
          Schema::table('users', function(Blueprint $table)
          {
              $table-&gt;increments('user_id');
              $table-&gt;string('user_email');
              $table-&gt;timestamp('user_created');
              $table-&gt;timestamp('user_modified');
              $table-&gt;timestamp('user_deleted');
              $table-&gt;timestamp('user_lastlogin');
              $table-&gt;timestamp('user_locked');
  
              $table-&gt;foreign('location_id')
                  -&gt;references('id')-&gt;on('locations');
              //-&gt;onDelete('cascade');
          });
      }
  </code></pre>
  
  <p>Migration Location</p>
  
  <pre><code>public function up()
      {
          Schema::table('locations', function(Blueprint $table)
          {
              $table-&gt;primary('location_id');
              $table-&gt;float('location_latitude');
              $table-&gt;float('location_longitude');
              $table-&gt;string('location_desc');
              $table-&gt;string('location_type');
          });
      }
  </code></pre>
  
  <p>Model User</p>
  
  <pre><code>public function location()
      {
          return $this-&gt;belongsTo('Location');
      }
  </code></pre>
  
  <p>Model Location</p>
  
  <pre><code> public function user()
      {
          return $this-&gt;hasOne('User');
      }
  </code></pre>
  
  <p>Controller</p>
  
  <pre><code>public function store()
      {
          $input = Input::all();
  
          $rules = array('user_email' =&gt; 'required|unique:users|email');
  
          $v = Validator::make($input, $rules);
  
          if($v-&gt;passes())
          {
              $user = new User();
              $location = new Location();
  
              $user-&gt;user_email = $input['user_email'];
              //$user-&gt;location_id = $input['location_id'];
  
              $location-&gt;location_latitude = $input['location_latitude'];
              $location-&gt;location_longitude = $input['location_longitude'];
  
              $user-&gt;save();
              $location-&gt;save();
      }
  </code></pre>
  
  <p>I can't seem to find what I'm doing wrong. Obviously there is something wrong with the foreign key. </p>