Having my 3rd topic related to this, and this is SO interesting because like, this is what I been wanting to explain and is bugging me, and like I said it's very interesting but also funny, lol !!
All im trying to do here is run: php artisan migrate:fresh --seed so I can run my DatabaseSeeder.php code, and I have 2 types of set up's basically and what works in 1 doesn't work with the other:
- locally
- deployed
DatabaseSeeder.php
public function run()
{
Status::factory()->create(['name' => 'Closed', 'classes' => 'bg-red text-white']);
Status::factory()->create(['name' => 'Open', 'classes' => 'bg-gray-200 text-gray-900']);
Status::factory()->create(['name' => 'Considering', 'classes' => 'bg-purple text-white w-32']);
Status::factory()->create(['name' => 'In Progress', 'classes' => 'bg-yellow text-gray-900 h-10']);
Status::factory()->create(['name' => 'Implemented', 'classes' => 'bg-green text-white w-32']);
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();
}
Locally, this doesn't work, but Heroku likes it.
return [
'user_id' => User::factory(),
'status_id' => Status::factory(),
'category_id' => Category::factory(),
'title' => ucwords($this->faker->words(4, true)),
'description' => $this->faker->paragraph(5),
];
Produces this error, locally:
SQLSTATE[HY000]: General error: 1364 Field 'classes' doesn't have a default value (SQL: insert into `statuses` (`name`, `updated_at`, `created_at`) values (est eos, 2021-10-15 23:22:28, 2021-10-15 23:22:28))
Gets solved like this, but Heroku doesn't like this:
'status_id' => $this->faker->numberBetween(1, 5),
'category_id' => $this->faker->numberBetween(1, 4),
And this is the message on Heroku:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint f
ails (`heroku_74d4fb19f97e161`.`ideas`, CONSTRAINT `ideas_category_id_foreign` FOREIGN KEY (`category_id`) REFEREN
CES `categories` (`id`))
As you can see im kind of juggling between the 2 setups and I don't want to be doing that anymore, Im just beginning to notice what works on both setups, im not able to know why. The point of doing the seed is to have some kind of default data that will be used on the project, like categories and statuses, those most likely will not change in the near future, so instead of inserting those records I think it's nice to have a project with those rows already populated.