Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

warpig's avatar
Level 12

Can't run seed on heroku

I tried to run heroku run -a playazchoice php artisan migrate:fresh --seed on the Heroku CLI but it's not letting me, why?

In Connection.php line 703:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`heroku_74d4fb1  
  9f97e161`.`ideas`, CONSTRAINT `ideas_category_id_foreign` FOREIGN KEY (`category_id`) REFERENCES `categories` (`id`)) (SQL: insert into  
   `ideas` (`user_id`, `category_id`, `title`, `description`, `slug`, `updated_at`, `created_at`) values (5, 4, Itaque Molestiae Et Natus  
  , Quas nesciunt fugit explicabo. Odio rem qui dolorum adipisci minus et. Sint laborum ut quidem ratione quia assumenda est. Doloremque   
  labore dolor quia neque cupiditate. Totam qui laboriosam doloribus non facilis placeat harum. Nesciunt magni aut nobis sed. In magnam a  
  ccusamus sit qui aperiam rerum., itaque-molestiae-et-natus, 2021-10-11 22:07:35, 2021-10-11 22:07:35)) 
In Connection.php line 492:
                                                                                                                                           
  SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`heroku_74d4fb1  
  9f97e161`.`ideas`, CONSTRAINT `ideas_category_id_foreign` FOREIGN KEY (`category_id`) REFERENCES `categories` (`id`))

Also when I was testing this:

    /** @test */
    public function list_of_ideas_show_on_main_page()
    {
        
        $categoryOne = Category::factory()->create(['name' => 'Category 1']);
        $categoryTwo = Category::factory()->create(['name' => 'Category 2']);
        
        $ideaOne = Idea::factory()->create([
            'title' => 'My First Idea',
            'category_id' => $categoryOne->id,
            'description' => 'Description of my first idea',
        ]);

        $ideaTwo = Idea::factory()->create([
            'title' => 'My Second Idea',
            'category_id' => $categoryTwo->id,
            'description' => 'Description of my second idea',
        ]);

        $response = $this->get(route('idea.index'));

        $response->assertSuccessful();
        $response->assertSee($ideaOne->title);
        $response->assertSee($ideaOne->description);
        $response->assertSee($categoryOne->name);
        $response->assertSee($ideaTwo->title);
        $response->assertSee($ideaTwo->description);
        $response->assertSee($categoryTwo->name);
    }

I had this error:

1) Tests\Feature\ShowIdeasTest::single_idea_shows_correctly_on_the_show_page
Illuminate\Database\QueryException: SQLSTATE[23000]: Integrity constraint violation: 19 FOREIGN KEY constraint failed (SQL: insert into "ideas" ("user_id", "category_id", "title", "description", "slug", "updated_at", "created_at") values (1, 2, My First Idea, Description of my first idea, my-first-idea, 2021-10-11 21:50:52, 2021-10-11 21:50:52))
0 likes
7 replies
Kikismedia's avatar

the error you are getting is coming from your idea migration , let your category migration come first before the idea migration then try seeding again

warpig's avatar
Level 12

@Kikismedia Yes, that's the way is setup right now, I can run heroku run -a playazchoice php artisan migrate:fresh and i will have no errors but it happens when I try to seed it

martinbean's avatar

@warpig Read your error message. A foreign key constraint is failed, so you’re trying to insert a row related to a record that doesn’t exist.

warpig's avatar
Level 12

@martinbean I can see that but I don't understand why after running fresh it doesn't work, what do I do?

warpig's avatar
Level 12

2021_10_08_221217_create_ideas_table

        Schema::create('ideas', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained();
            $table->foreignId('category_id')->constrained();
            $table->string('title');
            $table->string('slug')->nullable();
            $table->text('description');
            $table->timestamps();
        });

so the seed is not inserting a valid id value?

DatabaseSeeder.php

    public function run()
    {
        Category::factory()->create(['name' => 'Category 1']);
        Category::factory()->create(['name' => 'Category 2']);
        Category::factory()->create(['name' => 'Category 3']);
        Category::factory()->create(['name' => 'Category 4']);
        
        Idea::factory(30)->create();
    }
warpig's avatar
Level 12

@frankielee

    public function definition()
    {
        return [
            'user_id' => User::factory(),
            'category_id' => $this->faker->numberBetween(1, 4),
            'title' => ucwords($this->faker->words(4, true)),
            'description' => $this->faker->paragraph(5),
        ];
    }

?

Please or to participate in this conversation.