2013-12-20 8 views
7

Ich weiß, dass diese Frage bereits gestellt wurde, aber ich kann immer noch nicht finden, was ich tue falsch.SQLSTATE [23000]: Verletzung Integritätsbedingung: 1452 Hinzufügen oder Aktualisieren einer untergeordneten Zeile: eine Fremdschlüsseleinschränkung schlägt fehl - Laravel

Ich benutze das Framework Laravel.

Ich habe 2 Tabellen (Benutzer und Standorte). Wenn ich einen Benutzer erstellt werden soll, erhalte ich die Fehlermeldung:

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',))

Tabelle Benutzer

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; 

Tabelle Standorte

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; 

Migration Benutzer

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 Ort

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

Modell Benutzer

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

Modell Location

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

-Controller

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

ich nicht scheinen, kann zu dem, was ich falsch mache. Offensichtlich stimmt etwas mit dem Fremdschlüssel nicht.

Antwort

11

Eine Benutzerzeile benötigt einen Verweis auf einen gültigen Speicherort. Dieser Speicherort muss verfügbar sein, bevor der Benutzer gespeichert wird. Speichern Sie zuerst den Speicherort, speichern Sie den Benutzer und entfernen Sie die $user->location_id Zeile und Sie sind fertig.

+7

das ist seltsam, aber in meinem Fall habe ich Zeilen in der Constraint-Tabelle vor dem Einfügen, aber es immer noch diesen Fehler zu werfen. –

0

diejenigen Entwickler, die diese Fehler konfrontiert:

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))

Erste Speichern der Benutzerdatensatz des Benutzers versuchen dann Datensatz einfügen dann in die Datenbank leicht eingefügt.

KERNPUNKT

zwei Tabellen: ein Artikel und andere Benutzer. Ein Benutzer hat viele Artikel, aber kein Artikel hat viele Benutzer. Der Fremdschlüssel wird also in Richtung Artikeltisch verschoben. Wenn die Benutzertabelle keinen Datensatz enthält, tritt derselbe Fehler auf, der zuvor aufgetreten ist.

Dadurch können wir dieses Problem leicht lösen.

Verwandte Themen