0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

General error: 1364 Field 'カラム名' doesn't have a default valueのエラー対処方法

Posted at

エラー概要

storeメソッドでDBへデータを挿入する際に、
エラー「General error: 1364 Field 'カラム名' doesn't have a default value ...」
が出た。

結論

migrate時にカラムのnullを許容していなかった。

対応

データ挿入前だったので、
nullable()を追記して、ロールバックしてmigrateをやり直した。

ターミナル
$ php artisan migrate:status
$ php artisan migrate:rollback --step=1
migrationファイル
<?php

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

class AddImgToDbnameTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('dbname', function (Blueprint $table) {
-            $table->string("file_name")->after('content');
+            $table->string("file_name")->after('content')-->nullable();

-            $table->string("file_path")->after('content');
+            $table->string("file_path")->after('content')->nullable();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('dbname', function (Blueprint $table) {
            $table->dropColumn("file_name");
            $table->dropColumn("file_path");
        });
    }
}
ターミナル
$ php artisan migrate 
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?