Last Updated: 2021-05-15
I ran into errors when seeding the database in Laravel.
<?php // AdvisorSeeder.php DB::table('advisors')->insert([ 'id' => 1, 'user_id' => 4, "gender" => "female", "title" => "Taxwoman", ]) // UserSeeder.php DB::table('users')->insert([ 'id' => 4, 'email' => "email@example.com", ])
Later, when I had a test setup that called the
AdvisorSeeder, it failed. That
was because it referenced a non-existent foreign key, the user with
needed to have already seeded the users table for this connection to a foreign
key to work.
The foreign key targets must be seeded before the records that reference the foreign keys.