Anthonyx82's avatar

Laravel seeder dont fill table with foreing keys

I'm working on a Laravel project, and I have the following migrations:

public function up(): void
{
    Schema::create('customers', function (Blueprint $table) {
        $table->id('customer_id');
        $table->string('first_name', 50);
        $table->string('last_name', 50);
        $table->string('address', 100)->nullable();
        $table->string('phone', 15)->nullable();
        $table->string('email', 50)->unique();
        $table->timestamp('registration_date')->default(DB::raw('CURRENT_TIMESTAMP'));
        $table->timestamps();
    });
}

/**
 * Reverse the migrations.
 */
public function down(): void
{
    Schema::dropIfExists('customers');
}
public function up(): void
{
    Schema::create('vehicles', function (Blueprint $table) {
        $table->id('vehicle_id');
        $table->foreignId('customer_id')->constrained('customers')->onDelete('cascade');
        $table->string('make', 50);
        $table->string('model', 50);
        $table->year('year');
        $table->string('license_plate', 15)->unique();
        $table->string('vin', 17)->unique();
        $table->string('color', 20)->nullable();
        $table->timestamp('registration_date')->default(DB::raw('CURRENT_TIMESTAMP'));
        $table->timestamps();
    });
}

/**
 * Reverse the migrations.
 */
public function down(): void
{
    Schema::dropIfExists('vehicles');
}

As you can see in the vehicles table, there is a foreign key referencing the customers table. I'll show you the relationship in the models:

And finally, the factories:

Apparently, the following error is occurring:

SQLSTATE[HY000]: General error: 1 foreign key mismatch - "vehicles" referencing "customers" (Connection: sqlite, SQL: insert into "vehicles" ("customer_id", "make", "model", "year", "license_plate", "vin", "color", "registration_date", "updated_at", "created_at") values (11, Wilderman Group, nostrum, 1970, VRF-4423, FVKRDKQWQHGDVFWBP, purple, 2019-01-16, 2024-11-13 18:30:33, 2024-11-13 18:30:33))

This error is due to the attempt to add a foreign key in the vehicles table for a customer with ID 11, which clearly does not exist because only 10 customers are created for 15 vehicles. I tried "forcing" the error to go away by looping through all the created vehicles and assigning them to existing customers, but that led to another error. I think there’s likely something wrong here that would be much simpler to fix than trying to solve it in a way the framework wasn't designed for. If anyone knows what might be wrong, I’d appreciate the help.

P.S. I’m using Laravel 11

0 likes
3 replies
Nakov's avatar

And can you show how are you creating the models with the factory?

Because just running

Vehicle::factory()->count(10)->create();

should create both vehicle and customer. In case you have existing customer and you want to add vehicles for that specific customer, than

$customer = Customer::factory()->create();

Vehicle::factory()->forCustomer($customer)->count(10)->create();

this will create 10 vehicles for the same customer.

Anthonyx82's avatar

@Nakov Yeah im just running

Vehicle::factory()->count(10)->create();

But i dont want to create 10 vehicles for the same customer i want to distribute it into the costumers

Please or to participate in this conversation.