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

warpig's avatar
Level 12

Heroku: Cannot add or update a child row: a foreign key constraint fails

Ever since I changed the names of 2 migrations when I run this: php artisan migrate:fresh --seed I get the following:

  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`)) 

Any number of "ideas" is bound to any of the 4 existing categories in the database.

DatabaseSeeder

    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();
    }

I don't understand this problem, currently I have 4 categories, 30 users 0 ideas.

2021_10_07_221217_create_categories_table

        Schema::create('categories', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->timestamps();
        });

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();
        });

At this point is all fake data, do I need to truncate the database to start completely from scratch and move on with this error? Thanks.

EDIT: IdeaFactory.php

    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),
        ];
    }
0 likes
5 replies
Nakov's avatar
Nakov
Best Answer
Level 73

And how do you tell the Idea which category to use? In your IdeaFactory, do you set the category, since clearly you are not doing so through the seeder.

So in the definition you could do:

'category_id' => Category::factory()

which will create a category for each Idea, or you could use

$category = Category::factory()->create(['name' => 'Category 4']);
        
Idea::factory(30)->for($category)->create();

given that you have the relationship in your models setup.

1 like
warpig's avatar
Level 12

@Nakov Oh, so you're saying I can do this via tinker:

Idea::factory(30)->for($category)->create();

Or this:

'category_id' => Category::factory()

through the IdeaFactory? Updated the initial post with what I have on my IdeaFactory

Nakov's avatar

@warpig You can do it using tinker, but you'll have to create the category first. And I wouldn't use this $this->faker->numberBetween(1, 4) in a factory whenever I have a foreign key, since you might not know the correct IDs, you are doing okay for the user_id so do the same for the Category.

warpig's avatar
Level 12

@Nakov Yeah it's funny because its creating like 30 + records of the categories.. I guess I should create them better with tinker, like if I encounter this problem again, I think I should empty the database and then create the 4 categories, makes sense... SQL will complain if I don't because there will be in fact no value to bind with the ideas table when it tries to hit that row.. Yeah im following this https://laracasts.com/series/build-a-voting-app , André has no problem locally, me neither, it's just on the server. Thanks.

warpig's avatar
Level 12

hey @nakov, can I use this in my IdeaFactory?

$category = Category::factory()->create(['name' => 'Category 4']);
        
Idea::factory(30)->for($category)->create();

Right now it looks like this:

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

And i can't get that to work in Heroku so maybe there's another way?

Please or to participate in this conversation.