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),
];
}