title: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'user_id' cannot be null?
body: <p>I got this error when try to seed database.</p>
  <p>Laravel 7.</p>
  <h3>BlogPost Model</h3>
  <pre><code>class BlogPost extends Model
  {
      protected $fillable = [
          'title',
          'slug',
          'user_id',
          'category_id',
          'excerpt',
          'content_raw',
          'content_html',
          'is_published',
          'published_at',
          'updated_at',
          'created_at',
      ];
  
      public function category()
      {
          return $this-&gt;belongsTo(BlogCategory::class);
      }
  
      public function user()
      {
          return $this-&gt;belongsTo(User::class);
      }
  }
  
  </code></pre>
  <h3>User model</h3>
  <pre><code>class User extends Authenticatable
  {
      use Notifiable;
  
      /**
       * The attributes that are mass assignable.
       *
       * @var array
       */
      protected $fillable = [
          'name', 'email', 'password',
      ];
  
      /**
       * The attributes that should be hidden for arrays.
       *
       * @var array
       */
      protected $hidden = [
          'password', 'remember_token',
      ];
  
      /**
       * The attributes that should be cast to native types.
       *
       * @var array
       */
      protected $casts = [
          'email_verified_at' =&gt; 'datetime',
      ];
  }
  
  </code></pre>
  <h3>User migration</h3>
  <pre><code>        Schema::create('users', function (Blueprint $table) {
              $table-&gt;id();
              $table-&gt;string('name');
              $table-&gt;string('email')-&gt;unique();
              $table-&gt;timestamp('email_verified_at')-&gt;nullable();
              $table-&gt;string('password');
              $table-&gt;rememberToken();
              $table-&gt;timestamps();
          });
  
  </code></pre>
  <h3>BlogPost migration</h3>
  <pre><code>        Schema::create('blog_posts', function (Blueprint $table) {
              $table-&gt;increments('id');
              $table-&gt;unsignedInteger('category_id');
              $table-&gt;foreignId('user_id')-&gt;constrained();
              $table-&gt;string('title');
              $table-&gt;string('slug')-&gt;unique();
              $table-&gt;text('excerpt')-&gt;nullable();
              $table-&gt;text('content_raw');
              $table-&gt;text('content_html');
              $table-&gt;boolean('is_published')-&gt;default(false)-&gt;index();
              $table-&gt;timestamp('published_at')-&gt;nullable();
              $table-&gt;foreign('category_id')-&gt;references('id')-&gt;on('blog_categories');
              $table-&gt;timestamps();
          });
  
  </code></pre>
  <h3>User seeder</h3>
  <pre><code>class UserTableSeeder extends Seeder
  {
      public function run()
      {
          $users = [
              [
                  'name' =&gt; 'Author',
                  'email' =&gt; 'seriiburduja@mail.ru',
                  'password' =&gt; bcrypt('some1234')
              ],
              [
                  'name' =&gt; 'Admin',
                  'email' =&gt; 'seriiburduja@gmail.com',
                  'password' =&gt; bcrypt('some1234')
              ]
          ];
  
          DB::table('users')-&gt;insert($users);
      }
  }
  
  </code></pre>
  <h3>BlogPost Factory</h3>
  <pre><code>$factory-&gt;define(BlogPost::class, function (Faker $faker) {
      $title = $faker-&gt;sentence(rand(3, 8), true);
      $text = $faker-&gt;realText(rand(1000, 4000));
      $isPublished = rand(1, 5) &gt; 1;
      $createdAt = $faker-&gt;dateTimeBetween('-6 months', '-1 day');
  
      return [
          'category_id' =&gt; rand(1, 10),
          'user_id' =&gt; 1,
          'title' =&gt; $title,
          'slug' =&gt; Str::slug($title),
          'excerpt' =&gt; $faker-&gt;text(rand(100, 400)),
          'content_raw' =&gt; $text,
          'content_html' =&gt; $text,
          'is_published' =&gt; $isPublished,
          'published_at' =&gt; $isPublished ? $faker-&gt;dateTimeBetween('-6 months', '-1day') : null,
          'created_at' =&gt; $createdAt,
          'updated_at' =&gt; $createdAt
      ];
  });
  
  </code></pre>
  <h3>DatabaseSeeder</h3>
  <pre><code>class DatabaseSeeder extends Seeder
  {
      /**
       * Seed the application's database.
       *
       * @return void
       */
      public function run()
      {
          $this-&gt;call(UserTableSeeder::class);
          $this-&gt;call(BlogCategorySeeder::class);
          factory(BlogPost::class, 1)-&gt;create();
      }
  }
  
  </code></pre>
  <p>When i run <code>php artisan migrate:fresh --seed</code> i got this error.</p>
  <p>Tables users and blog_categories seeds successfully, but error appear for blog_categories.</p>
  <p>I don't understand why.</p>
  <p>Field <strong>user_id</strong> exists in $fillable in BlogPost Model.</p>
  <p>If i change migration for blog_posts and add a nullable for user_id, than seed work, but user_id is null. But i don't need that.</p>
  <p>Thansk in advance.</p>