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

rhand's avatar
Level 6

Seeder for Articles fails on missing user_id value

Decided to add a factory for my basic Jetstream Inertia app running Docker Sail. But when I do run this factory I get this error

docker exec -it 7dbeb42826d3009b1b927a83dfaf9e62a2edf172c220e425fcc4ff3809096572 php artisan migrate:fresh --seed

  Dropping all tables ............................................. 133ms DONE

   INFO  Preparing database.  

  Creating migration table ......................................... 29ms DONE

   INFO  Running migrations.  

  2014_10_12_000000_create_users_table ............................. 37ms DONE
  2014_10_12_100000_create_password_resets_table ................... 25ms DONE
  2014_10_12_200000_add_two_factor_columns_to_users_table .......... 12ms DONE
  2019_08_19_000000_create_failed_jobs_table ....................... 41ms DONE
  2019_12_14_000001_create_personal_access_tokens_table ............ 46ms DONE
  2020_05_21_100000_create_teams_table ............................. 22ms DONE
  2020_05_21_200000_create_team_user_table ......................... 25ms DONE
  2020_05_21_300000_create_team_invitations_table .................. 53ms DONE
  2021_07_12_030017_create_sessions_table .......................... 61ms DONE
  2021_07_12_082029_create_articles_table .......................... 44ms DONE

   INFO  Seeding database.  


   Illuminate\Database\QueryException 

  SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value (SQL: insert into `articles` (`title`, `content`, `updated_at`, `created_at`) values (Architecto magnam velit sapiente quod velit eaque vitae molestias., Dolores et facere est. Minus magnam quasi dolor sit voluptatem omnis. Non rerum harum rerum amet sint assumenda., 2023-02-12 02:11:39, 2023-02-12 02:11:39))

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:760
    756▕         // If an exception occurs when attempting to run a query, we'll format the error
    757▕         // message to include the bindings with SQL, which will make this exception a
    758▕         // lot more helpful to the developer instead of just the database's errors.
    759▕         catch (Exception $e) {
  ➜ 760▕             throw new QueryException(
    761▕                 $query, $this->prepareBindings($bindings), $e
    762▕             );
    763▕         }
    764▕     }

      +16 vendor frames 
  17  database/seeders/DatabaseSeeder.php:16
      Illuminate\Database\Eloquent\Factories\Factory::create()

      +35 vendor frames 
  53  artisan:35
      Illuminate\Foundation\Console\Kernel::handle()

Factory database/factories/ArticleFactory.php is

<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Article>
 */
class ArticleFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition()
    {
        return [
            'title' => $this->faker->sentence(6),
            'content' => $this->faker->paragraph(4),
        ];
    }
}

and included via database seeder:

<?php

namespace Database\Seeders;
use App\Models\Article;
use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        Article::factory(10)->create();
    }
}

Article Model is

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Article extends Model
{
    use HasFactory;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [

        'title', 'content',

    ];

    public function user()
    {
        return $this->belongsTo(User::class);
    }
}

How can I add user ids properly? Perhaps I need to add a user factory first?

0 likes
5 replies
webrobert's avatar

Yes. A factory for user

You may have one already?

Then just do User::factory() for the user Id in the other factory.

1 like
rhand's avatar
Level 6

@webrobert Yes I realized I already did but did not add to general database user so I did

<?php

namespace Database\Seeders;
use App\Models\Article;
use App\Models\User;
use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        User::factory(10)->create();
        Article::factory(10)->create();
    }
}

and ran all again. Then I hit

 INFO  Seeding database.  


   Illuminate\Database\QueryException 

  SQLSTATE[HY000]: General error: 1366 Incorrect integer value: 'Illum omnis quis assumenda. Corrupti illo excepturi ut nesciunt. Quisquam temporibus cumque vel maiores voluptate id sunt.' for column 'user_id' at row 1 (SQL: insert into `articles` (`title`, `content`, `user_id`, `updated_at`, `created_at`) values (Distinctio et libero a et facere labore sit., Incidunt in ut omnis sit autem atque a. Dolorem sed praesentium quia. Qui aut ut vero deserunt. Sed dolores exercitationem tempore dolore. Id error aut dolor fugit. Voluptate itaque incidunt reprehenderit tenetur., Illum omnis quis assumenda. Corrupti illo excepturi ut nesciunt. Quisquam temporibus cumque vel maiores voluptate id sunt., 2023-02-12 02:31:22, 2023-02-12 02:31:22))

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:760
    756▕         // If an exception occurs when attempting to run a query, we'll format the error
    757▕         // message to include the bindings with SQL, which will make this exception a
    758▕         // lot more helpful to the developer instead of just the database's errors.
    759▕         catch (Exception $e) {
  ➜ 760▕             throw new QueryException(
    761▕                 $query, $this->prepareBindings($bindings), $e
    762▕             );
    763▕         }
    764▕     }

      +16 vendor frames 
  17  database/seeders/DatabaseSeeder.php:18
      Illuminate\Database\Eloquent\Factories\Factory::create()

      +35 vendor frames 
  53  artisan:35
      Illuminate\Foundation\Console\Kernel::handle()

users did get added, just not the articles.. Content column is TEXT in articles table. So guess I need to deal with type first somehow .

webrobert's avatar
Level 51

@rhand

   public function definition()
    {
        return [
            'title' => $this->faker->sentence(6),
            'content' => $this->faker->paragraph(4),
‘User_id’ => User::factory()
        ];
    }

You can just run this factory. And it will create users for the discussions. Fix my syntax. On my phone.

1 like
rhand's avatar
Level 6

@webrobert Yes, that part

...
public function definition()
    {
        return [
            'title' => $this->faker->sentence(6),
            'content' => $this->faker->paragraph(4),
            'user_id' => User::factory()
        ];
    }
...

with loading user id based on factory did the trick:

docker exec -it 7dbeb42826d3009b1b927a83dfaf9e62a2edf172c220e425fcc4ff3809096572 php artisan migrate:fresh --seed

  Dropping all tables ....................................................... 115ms DONE

   INFO  Preparing database.  

  Creating migration table ................................................... 22ms DONE

   INFO  Running migrations.  

  2014_10_12_000000_create_users_table ....................................... 24ms DONE
  2014_10_12_100000_create_password_resets_table ............................. 22ms DONE
  2014_10_12_200000_add_two_factor_columns_to_users_table .................... 12ms DONE
  2019_08_19_000000_create_failed_jobs_table ................................. 22ms DONE
  2019_12_14_000001_create_personal_access_tokens_table ...................... 33ms DONE
  2020_05_21_100000_create_teams_table ....................................... 20ms DONE
  2020_05_21_200000_create_team_user_table ................................... 20ms DONE
  2020_05_21_300000_create_team_invitations_table ............................ 59ms DONE
  2021_07_12_030017_create_sessions_table .................................... 48ms DONE
  2021_07_12_082029_create_articles_table .................................... 40ms DONE

   INFO  Seeding database.  

That and loading in user factory first. Thanks a lot!

Please or to participate in this conversation.