0
votes

Illuminate\Database\QueryException SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (pride-express.travel_planes, CONSTRAINT travel_planes_circuit_id_foreign FOREIGN KEY (circuit_id) REFERENCES circuits (id) ON DELETE CASCADE ON UPDATE CASCADE) (SQL: insert into travel_planes (car_id, user_id, title, is_reservation_open, departure_time, updated_at, created_at) values (1, 1, Roles, 1, 2021-06-10 07:02:00, 2021-06-10 04:06:39, 2021-06-10 04:06:39))

Here is my circuit table migration:

Schema::create('circuits', function (Blueprint $table) {
        $table->id();
        $table->string('circuit_slug')->unique();
        $table->string('departure');
        $table->string('destination');
        $table->timestamps();
    });

Circuit model:

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Circuit extends Model
{
    use HasFactory;


    /**
    * Get the .. that owns the comment.
    */
   public function travelPlane()
   {
       return $this->hasMany(TravelPlane::class);
   }
}

Circuit table Print Screen

And below is my travel_planes migration table:

Schema::disableForeignKeyConstraints();
    Schema::create('travel_planes', function (Blueprint $table) {
        $table->id();
        $table->string('title')->unique();
        $table->string('slug')->unique();
        $table->unsignedBigInteger('car_id');
        $table->foreign('car_id')
        ->references('id')
        ->on('cars')
        ->onDelete('cascade')
        ->onUpdate('cascade');

        $table->unsignedBigInteger('circuit_id');
        $table->foreign('circuit_id')
        ->references('id')
        ->on('circuits')
        ->onDelete('cascade')
        ->onUpdate('cascade');
        $table->timestamp('departure_time');
        $table->unsignedBigInteger('user_id');
        $table->foreign('user_id')
            ->references('id')
            ->on('users')
            ->onDelete('cascade')
            ->onUpdate('cascade');
        $table->boolean('is_reservation_open')->default(true);
        $table->timestamps();

    });

TravelPlanes model:

<?php

namespace App\Models;

use Illuminate\Support\Facades\Auth;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class TravelPlane extends Model{
use HasFactory;


   /**
   * Get the Trajectory for the travel.
   */
    public function circuit()
    {
        return $this->belongsTo(Circuit::class);
    }


 }

Travel planes Print screen

NB: data in the Travel planes table was generated by faker.

This error occured when I tried to add a new line in my travel_plane table using voyager admin panel. Any help please ?

2

2 Answers

0
votes

This error Comes from travel_plane table witch has a foreign Key on witch you try to insert a value witch IS not available in the parent table.

0
votes

I have solve my problem. Here is what is the issue:

In my travel_plane BREAD cretor I should check add and edit part at both the default column name and the relationship created by voyager .

See in this tow print screen what I mean.

print screen for the default table column print screen for the relationship column