2013-12-20 4 views
7

この質問は既に尋ねられていますが、私はまだ何をしているのかわかりません違う。SQLSTATE [23000]:整合性制約違反:1452子行を追加または更新できません:外部キー制約が失敗しました - Laravel

私はフレームワークLaravelを使用しています。

私は2つのテーブル(ユーザーと場所)を持っています。

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (festival_aid . users , CONSTRAINT fk_users_locations1 FOREIGN KEY (location_id) REFERENCES locations (location_id) ON DELETE CASCADE ON UPDATE NO ACTION) (SQL: insert into users (user_id , user_email , location_id) values (?, ?, ?)) (Bindings: array (0 => '1', 1 => '[email protected]', 2 => '1',))

表のユーザー

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; 

表の場所

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; 

移行ユーザー

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'); 
     }); 
    } 

移行所在地:私は、ユーザーを作成したい場合は、私は、エラーメッセージが表示されます

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'); 
     }); 
    } 

モデルユーザー

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

モデル場所

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

コントローラ

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(); 
    } 

私は私が間違ってやっているものを見つけるように見えることはできません。明らかに、外部キーに問題があります。

答えて

11

ユーザー行には有効な場所への参照が必要です。 ユーザーを保存する前に、その場所を使用可能にする必要があります。 まず、場所を保存してからユーザーを保存し、$user->location_id行のコメントを外してください。このエラーに直面

+7

私の場合、私は挿入する前に制約テーブルに行がありますが、それでもこのエラーはスローされます。 –

0

これらの開発者:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row:
a foreign key constraint fails (laravel . articles , CONSTRAINT articles_user_id_foreign FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE)
(SQL: insert into articles (title , user_id , body , updated_at , created_at) values (rao, 1, sflkkjk, 2016-03-01 20:45:32, 2016-03-01 20:45:32))
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row:
a foreign key constraint fails (laravel . articles , CONSTRAINT articles_user_id_foreign FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE)
(SQL: insert into articles (title , user_id , body , updated_at , created_at) values (rao, 1, sflkkjk, 2016-03-01 20:45:32, 2016-03-01 20:45:32))

まず保存したユーザのユーザレコードは、その後、簡単にデータベースに挿入されたレコードを挿入しようとします。

KEY POINT

2つのテーブル:1の記事で、他のユーザーです。ユーザーには多数の記事がありますが、記事には多数のユーザーがいます。外部キーは記事テーブルの方に移動します。ユーザーテーブルにレコードがない場合は、前の同じエラーが発生します。

このようにすれば、簡単にこの問題を解決できます。