mardi 18 août 2020

Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails error

this is my code . It adding some records. But at some point giving me error.

foreach($data as $obj){

  if($obj && $obj->stateId && $obj->countryId) {
     MasterCity::create(array(
        'name' => $obj->name,
        'state_id' => $obj->stateId,
        'country_id' => $obj->countryId
    ));
  }
}

I am getting 2 errors:

 C:\ashish\backend\laravel-backend\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\PDOStatement.php:129
      Doctrine\DBAL\Driver\PDOException::("SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`blue_rickshaw`.`mast
er_cities`, CONSTRAINT `master_cities_state_id_foreign` FOREIGN KEY (`state_id`) REFERENCES `master_states` (`id`))")

  2   C:\ashish\backend\laravel-backend\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\PDOStatement.php:127
      PDOException::("SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`blue_rickshaw`.`master_cities`, CONSTRAIN
T `master_cities_state_id_foreign` FOREIGN KEY (`state_id`) REFERENCES `master_states` (`id`))")


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire