【问题标题】:SQLSTATE[HY000]: General error: 1364 Field 'client_id' doesn't have a default valueSQLSTATE [HY000]:一般错误:1364 字段“client_id”没有默认值
【发布时间】:2021-01-04 17:34:44
【问题描述】:

我试图通过请求帖子发送“client_id”将帖子与客户端相关联,然后当我创建它时,它给出了这个异常: SQLSTATE[HY000]:一般错误:1364 字段“client_id”没有默认值(SQL:插入postscontentupdated_atcreated_at)值(sadadad,2020-09-17 2020-09-17 18:26:38 18:26:38))

<form role="form" action="{{ route('posts.store') }}" method="POST" enctype="multipart/form-data">
    @csrf
    {{ method_field('POST') }}

    @include('includes.errors')
    <input type="hidden" name="client_id" value="{{ $client->id }}">

    <textarea type="text" class="form-control form-control-md" placeholder="what's in your mind ?" name="content"></textarea>

    <div style="margin: 10px 0;">
        <input type="file" hidden id='upload-photo' name="image">
        <label for="upload-photo" class="post-adds" ><i class="far fa-file-image fa-lg"></i></label>
        <input type="file" hidden id='upload-video' name="video">
        <label for="upload-video" class="post-adds" ><i class="far fa-file-video fa-lg"></i></label>
        <button class="btn btn-sm btn-outline-primary " type="submit" style="display: inline; float:right;">Post</button>
    </div>
</form>

migrations:

Schema::create('posts', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->text('content')->nullable();
            $table->integer('likes')->default(0);
            $table->integer('shares')->default(0);
            $table->string('image')->nullable();
            $table->string('video')->nullable();
            $table->timestamps();

            $table->integer('client_id')->unsigned();
            $table->foreign('client_id')->references('id')->on('clients')->onDelete('cascade')->onUpdate('cascade');
        });

controller:

$request->validate([
            'video'=> 'mimes:mp4| max:20000',
            'image'=>'image | max:2000',
            'content'=>'required',
        ]);

        $request_data = $request->except('video','image');

        if($request->image){
            $img = Image::make($request->image);

            $img->resize(300, null, function ($constraint) {
                $constraint->aspectRatio();
            });

            $img->save(public_path('dist/img/posts/'.$request->image->hashName()),60);

            $request_data['image'] = $request->image->hashName();
        }

        if($request->video){
            $vid = $request->video;

            $vid_name = rand().'.'.$vid->getClientOriginalExtension();

            $vid->move(public_path('dist/vid/posts'),$vid_name);

            $request_data['video'] = $vid_name;
        }

        Post::create($request_data);

        session()->flash('success',__('site.post_created_successfully'));
        return redirect()->route('posts.index');

【问题讨论】:

  • 尝试在数据库中将此列的默认值设置为空,然后尝试应该可以正常工作

标签: php laravel migration laravel-6


【解决方案1】:

您需要像这样在Post 模型中的fillable 数组中添加client_id

protected $fillable = [
    ...

    'client_id',

    ...
];

另一种解决方案

您可以添加您的Post 模型

protected $guarded = [];

这将使所有属性都可以批量分配

【讨论】:

    【解决方案2】:

    有一种可能,可以考虑,我经历过。

    1. client_id 字段不是nullable 列,因此它必须具有自动增量功能

    2. 由于client_id字段不是nullable列,请在输入新行时指定其内容

    根据 laravel 官方文档 (here),迁移中的 -&gt;unsigned() 函数只是将 UNSIGNED 属性添加到列中,而不添加 AUTO INCREMENT 属性。

    如果是这种情况,您有 2 个选择。

    1. -&gt;autoIncrement() 函数添加到client_id 列以进行迁移
    2. 将列类型更改为-&gt;foreignId('user_id')(即删除现有的-&gt;unsigned()函数)

    【讨论】:

      猜你喜欢
      • 2019-12-01
      • 2021-05-23
      • 2019-05-09
      • 2019-09-10
      • 2020-09-06
      • 2020-06-21
      • 2019-05-19
      • 2019-06-05
      • 2019-12-13
      相关资源
      最近更新 更多