简体   繁体   中英

Laravel 6 Seed DB When relation is on 1 table

I have one table which it has a column named "parent_id".

      Schema::create('yazilars', function (Blueprint $table) {
        $table->bigIncrements('id');
        .
        .
        .
        $table->unsignedBigInteger('parent_id')->nullable();
        $table->foreign('parent_id')->references('id')->on('yazilars');
        $table->timestamps();
    });

When I try to seed the db, I get following error. I know the error should be there because I am trying to relate the rows which are not created yet.

The error is:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

My Seeder file;

public function run(){
    $json = File::get("database/veriler/yazilar.json");
    $data = json_decode($json);
    foreach ($data as $obj) {
        Yazilar::create(array(
        'id' => $obj->id,
        .
        .
        .
        "yazilar_id" => $obj->yazilar_id
      ));
    }
}
database\seeds\DatabaseSeeder.php 

在此 DatabaseSeeder.php 中添加上面的父播种机

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM