SQLSTATE[23000]: Integrity constraint violation: 1452 Ne peut pas ajouter ou mettre à jour une ligne enfant: une contrainte de clé étrangère échoue - Laravel

Je sais que cette question a déjà été posée, mais je ne trouve toujours pas ce que je fais mal.

Je suis en utilisant le framework Laravel.

J'ai 2 tables (les Utilisateurs et les Lieux). Lorsque je veux créer un Utilisateur, j'obtiens le message d'erreur:

SQLSTATE[23000]: Integrity constraint violation: 1452 Impossible d'ajouter ou de
mise à jour d'une ligne enfant: une contrainte de clé étrangère échoue
(festival_aid.users, CONTRAINTE fk_users_locations1 DE CLÉ ÉTRANGÈRE
(location_id) RÉFÉRENCES locations (location_id) SUR SUPPRIMER
CASCADE on UPDATE NO ACTION) (SQL: insert into users (user_id,
user_email, location_id) values (?, ?, ?)) (Les liaisons: array ( 0 =>
'1', 1 => '[email protected]', 2 => '1', ))

Table Des Utilisateurs

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;

De La Table

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;

Utilisateur De Migration

public function up()
    {
        Schema::table('users', function(Blueprint $table)
        {
            $table->increments('user_id');
            $table->string('user_email');
            $table->timestamp('user_created');
            $table->timestamp('user_modified');
            $table->timestamp('user_deleted');
            $table->timestamp('user_lastlogin');
            $table->timestamp('user_locked');

            $table->foreign('location_id')
                ->references('id')->on('locations');
            //->onDelete('cascade');
        });
    }

Migration Emplacement

public function up()
    {
        Schema::table('locations', function(Blueprint $table)
        {
            $table->primary('location_id');
            $table->float('location_latitude');
            $table->float('location_longitude');
            $table->string('location_desc');
            $table->string('location_type');
        });
    }

Modèle De L'Utilisateur

public function location()
    {
        return $this->belongsTo('Location');
    }

Emplacement De Modèle

 public function user()
    {
        return $this->hasOne('User');
    }

Contrôleur

public function store()
    {
        $input = Input::all();

        $rules = array('user_email' => 'required|unique:users|email');

        $v = Validator::make($input, $rules);

        if($v->passes())
        {
            $user = new User();
            $location = new Location();

            $user->user_email = $input['user_email'];
            //$user->location_id = $input['location_id'];

            $location->location_latitude = $input['location_latitude'];
            $location->location_longitude = $input['location_longitude'];

            $user->save();
            $location->save();
    }

Je n'arrive pas à trouver ce que je fais mal. Il y a évidemment quelque chose de mal avec la clé étrangère.

OriginalL'auteur Gilko | 2013-12-20