jeudi 1 décembre 2016

Laravel 5.3 Eloquent foreign key constraint fails 1452

So others who have posted a similar issue have had responses saying set the $fillable array to contain the foreign key column. I have added brand_id to my fillable array but it makes no difference.

The migration adds a brand_id column to the fabrics table (each fabric has one brand):

Schema::table('es_fabrics', function ($table) {
    $table->integer('brand_id')->unsigned();
    $table->foreign('brand_id')->references('brand_id')->on('es_brands');
});

The column is created but when the migration comes to making the foreign key constraint I get the following error:

[Illuminate\Database\QueryException]
  SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`es-app`.`#sql-33d4_9e`, CONSTRAINT `es_fa
  brics_brand_id_foreign` FOREIGN KEY (`brand_id`) REFERENCES `es_brands` (`brand_id`)) (SQL: alter table `es_fabrics` add constraint `es_fabrics_brand_id_foreign` foreig
  n key (`brand_id`) references `es_brands` (`brand_id`))



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire