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

warpig's avatar
Level 12

Juggling between 2 setups, deployed v local, factories & seeds, (model instantiated or faker statements)

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:

  1. locally
  2. 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.

0 likes
2 replies
Snapey's avatar

You know the reason now.

One option is to change your primary key for these tables so that you specify the key instead of using autoincrement.

Status::factory()->create(['id' => 1, 'name' => 'Closed', 'classes' => 'bg-red text-white']);
Status::factory()->create(['id' => 2, 'name' => 'Open', 'classes' => 'bg-gray-200 text-gray-900']);
Status::factory()->create(['id' => 3, 'name' => 'Considering', 'classes' => 'bg-purple text-white w-32']);
Status::factory()->create(['id' => 4, 'name' => 'In Progress', 'classes' => 'bg-yellow text-gray-900 h-10']);
Status::factory()->create(['id' => 5, 'name' => 'Implemented', 'classes' => 'bg-green text-white w-32']);

So now your keys are deterministic

In your shoes, I would have a static model using something like Sushi rather than a database table. https://github.com/calebporzio/sushi

warpig's avatar
Level 12

@Snapey I ended up using a method inside the Idea model with the same functionality that you see here but instead of using rows to store these classes im only using: getStatusClasses() ... the names will remain in this Seeder along with the categories but as for the statuses I should just specify their ID's to have what you call as deterministic keys.

Please or to participate in this conversation.