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.

Laravelでテーブルの削除・変更を行った際のエラー(SQLSTATE[HY000]: General error: 1364 Field '〇〇' doesn't have a default value)

Last updated at Posted at 2023-04-20

バージョン

Laravel Framework 9.52.5
PHP 8.2.4

やりたかったこと

Laravelにてデータテーブルのカラムに(user_id)的なものを追加

エラー

その際にデフォルトのカラムであるnameを削除し忘れており

SQLSTATE[HY000]: General error: 1364 Field 'name' doesn't have a default value

と出た

解決策

自分はnameの方はもう使わない想定だったので

php artisan make:migration drop_column_users_column --table=users

でusers削除のためのマイグレーションを作成

users_column.php
<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            //
            $table->dropColumn('name'); #追加

        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('users', function (Blueprint $table) {
            //
            $table->boolean('name')->default(false); #追加

        });
    }
};

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?