2013-12-20 76 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

兩個表:一個是文章和其他由用戶。用戶有很多文章,但沒有文章有很多用戶。所以外鍵轉向了文章列表。如果用戶表沒有記錄,那麼你將面臨同樣的錯誤。

通過這樣做,我們可以輕鬆解決這個問題。

相關問題