1

I'm trying to seed my database with faker, but I have some issues with a foreign element. I have one and only user and I don't know its id. I want all the posts to have its id.
This is how I did this :

public function run()
{
    factory(App\User::class)->create();
    factory(App\Category::class, 3)->create();

    $user = \App\User::first();

    factory(App\Post::class, 10)->create(['author_id' => $user->id]);
}

And this is my post factory :

$factory->define(App\Post::class, function (Faker\Generator $faker) {
    return [
        'title' => $faker->unique()->sentence(5),
        'subtitle' => $faker->optional()->sentence(10),
        'markdown' => $faker->text(500),
        'draft' => $faker->boolean(),
        'category_id' => $faker->numberBetween(1, 6),
    ];
});

After a db:seed, this is the error I get (users and categories are well populated) :

  SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`laman`.`posts`, CO
  NSTRAINT `posts_author_id_foreign` FOREIGN KEY (`author_id`) REFERENCES `users` (`id`)) (SQL: insert into `posts` (`title`, `slug`, `subtit
  le`, `markdown`, `draft`, `category_id`, `author_id`, `html`, `published_at`, `updated_at`, `created_at`) values (title.,
   slug, , markdown, 0, 6, 1, html, 2017-04-25 20:08:34, 2017-04-25 20:08:34, 2017-04-25
  20:08:34))

Here the user id is 1 or it should be 9. Why?

Louis Etienne
  • 1,302
  • 3
  • 20
  • 37
  • It looks like this creates a new user every time, but you are calling for the first user in the table. Is this the case, or do you refresh your migrations? – Naltroc Apr 25 '17 at 20:32
  • I delete the user and the categories un phpmyadmin. Is there a better way to truncate table with relationships? – Louis Etienne Apr 25 '17 at 20:38
  • Using `php artisan migrate:refresh --seed` will completely rollback and migrate your database as well as populate the seeder file. This is good as long as you do not have valuable data in the database. It is generally good practice to use migrations for managing the database, though I've caught myself making the manual delete every once and a while. – Naltroc Apr 25 '17 at 21:40

1 Answers1

0

i recommend to seed your DB as follows:

  1. To avoid manually deleting db entries, populate your seeder with DB::table('YourDBTable')->truncate(); :

    public function run()
    {
        DB::table('user')->truncate();
        DB::table('posts')->truncate();
    
        factory(App\User::class)->create();
        factory(App\Category::class, 3)->create(); 
    
        $user = \App\User::first();
    
        factory(App\Post::class, 10)->create(['author_id' => $user->id]);
    }
    

Now everytime you run artisan command db:seed, you truncate your users table after which you are populating your db with fresh data and thus you are able to fetch the single one user, and the right one too, with the first() method.

  • Can't use this because of foreign key, but this solution : http://stackoverflow.com/questions/31192207/laravel-5-1-migration-and-seeding-cannot-truncate-a-table-referenced-in-a-foreig is working. So, thanks! – Louis Etienne Apr 26 '17 at 05:51