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

Asked 2 months ago, Updated 2 months ago, 4 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

 laravel-app/app/Post.php

use Illuminate\Database\Eloquent\Model;

class Post extensions Model
{
    /**
     * Obtain Users Who Own Post Data
     */
    public function user()
    {
     return$this->belongsTo('App\User');
    }
    
     // assignment authorization
     protected$fillable=[
        'name',
        'subject',
        'message', 
        'user_id',
        # 'category_id'
    ];

laravel-app/database/migrations/2021_03_10_101736_create_posts_table.php


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->bigIncrements('id');
            $table->timestamps();
            $table->string('is_deleted',4)->default('0');
            # $table-> integer('category_id');
            $table->string('subject');
            $table->text('message');
            $table->string('name');
            $table->unsignedBigInteger('user_id');# Added
            $table->foreign('user_id')->references('id')->on('users');# Added
        });
    }

laravel-app/database/seeds/PostsTableSeeder.php

use Illuminate\Database\Seeder;

class PostsTableSeeder extensions Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        factory(App\Post::class,50)
            ->create()
            ->each(function($post){
                $comments=factory(App\Comment::class,2) - > make();
                $post->comments()->saveMany($comments);
            }
        );
    }
}


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 user.id, 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")
      /var/www/html/larvel-app/vendor/larvel/framework/src/Illuminate/Database/Connection.php:458

  2PDOSstatement::execute()
      /var/www/html/larvel-app/vendor/larvel/framework/src/Illuminate/Database/Connection.php:458
end

Reference Articles

Login Features
https://note.com/yuki_biwako/n/n696cb97b64b7
Post Relationship
https://nodoame.net/archives/11628#vol9

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


© 2022 OneMinuteCode. All rights reserved.