Illuminate\Database\QueryException: SQLSTATE [HY000]: General error: 1364 Field

Asked 5 months ago, Updated 5 months ago, 16 views

What do you want to solve

I am creating a simple posting site like Twitter while referring to articles on the Internet.
The login function is being implemented and the dummy data of the user is being created.
We will proceed while checking the CRUD flow from listing to post implementation, and we will implement additional login functionality.

I added the 'user_id' column, but the error below appears.
There was no problem with the dummy data for posting until the login function.

Problems/errors encountered

 Illuminate\Database\QueryException: SQLSTATE [HY000]: General error: 1364 Field 
   Illuminate\Database\QueryException: SQLSTATE [HY000]: General error: 1364 Field 'user_id' does not have a default value (SQL:insert into `posts` (`created_at`, `updated_at`, `subject`, `message`, `name`) 11:02:132:132:132:132, She looked at the back of her hat, and said, "I'll have a lot of grass." Giovanni was looking at the back of her hat.The crane did not blow the whistle that it had done again."You're a rabbit, aren't you?" "You're all in the blue sky." I asked Campanella."No, the forest," said Jovanni, "don't be too busy, and take your white clothes off the river.", Yoshimoto Kyosuke))

Affected Source Codes


use Illuminate\Database\Eloquent\Model;

class Post extensions Model
     * Obtain Users Who Own Post Data
    public function user()
     // assignment authorization
        # 'category_id'


use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migration;

class CreatePostsTable extensions Migration
     * Run the migrations.
     * @return void
    public function up()
        Schema::create('posts', function(Blueprint$table){
            # $table-> integer('category_id');
            $table->unsignedBigInteger('user_id');# Added
            $table->foreign('user_id')->references('id')->on('users');# Added


use Illuminate\Database\Seeder;

class PostsTableSeeder extensions Seeder
     * Run the database seeds.
     * @return void
    public function run()
                $comments=factory(App\Comment::class,2) - > make();

What I tried myself

I rewritten the table, so I migrated:refresh.
Next, I wanted to create dummy data. (PostsTableSeeder, UsersTableSeeder)
The PostsTableSeeder failed.
I added, so there was a problem with the dummy data of my previous post.

$ phpartisan migrate —refresh
Dropped all tables successfully.
Migration table created successfully.
$  phpartisan db:seed

Exception trace:

  1 PDOException::("SQLSTATE [HY000]: General error: 1364 Field 'user_id' does not have a default value")


Reference Articles

Login Features
Post Relationship

php laravel

2022-09-30 11:23

1 Answers

Modify database/factories/PostFactory.php to include values in the user_id column.

Note: Larvel 7.x Database Test Relationship

2022-09-30 11:23

If you have any answers or tips

© 2023 OneMinuteCode. All rights reserved.