programing

MySql 오류: 1364 필드 'display_name'에 기본값이 없습니다.

randomtip 2022. 9. 13. 22:01
반응형

MySql 오류: 1364 필드 'display_name'에 기본값이 없습니다.

방금 MAMP 설치에서 Apache, MySql 및 PHP 네이티브 설치로 전환했습니다.모든 것이 정상적으로 동작하고 있습니다만, 새로운 환경에서 Web 앱을 사용하기 시작했는데, 갑자기 INSERT 명령어를 실행하면 다음과 같은 에러가 발생합니다.

SQLSTATE[HY000]: 일반 오류: 1364 필드 'display_name'에 기본값이 없습니다.

예전처럼 필드를 비워둘 수 없게 된 것 같습니다.MySql 버전 5.6.13을 사용하고 있습니다.

MySql에서 이 설정을 변경할 수 있는 방법이 있습니까?

MySQL은 대부분 STRICT 모드입니다.

실행해 보다SET GLOBAL sql_mode=''또는 my.cnf를 편집하여 설정되지 않았는지 확인합니다.STRICT_ALL_TABLES뭐 그런 거.

MySQL은 대부분의 경우STRICT이 모드는 반드시 나쁜 것은 아닙니다.버그나 오류를 조기에 특정하고 모든 것이 의도한 대로 동작하고 있다고 무작정 생각하지 않기 때문입니다.

열을 null로 변경합니다.

ALTER TABLE `x` CHANGE `display_name` `display_name` TEXT NULL

또는 기본값을 빈 문자열로 지정합니다.

ALTER TABLE `x` CHANGE `display_name` `display_name` TEXT NOT NULL DEFAULT ''

이러한 답변은 모두 좋은 방법이지만, 항상 DB로 이동하여 NULL로 설정한 기본값을 수정하고 싶지는 않다고 생각합니다.

app/User.php에 접속하여 수정하는 것이 좋습니다.protected $fillable따라서 등록에 사용되는 데이터 배열의 새 필드를 사용할 수 있습니다.

라고 하는 새로운 입력 필드를 추가했다고 합시다."first_name",당신의.$fillable는 다음과 같습니다.

protected $fillable = [ 'first_name', 'email', 'password',]; 

이걸로 끝이야.행운을 빕니다.

나는 또한 그 문제에 직면했고 이것을 전체적으로 해결할 수 있는 두 가지 방법이 있다.

  1. 첫 번째는 기본값을 null로 설정할 수 있다는 것입니다.예를 들어 보겠습니다.

    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('gender');
            $table->string('slug');
            $table->string('pic')->nullable();
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }
    

위의 예와 같이,nullable()그 기능을 위해서요.데이터 MySQL을 삽입할 때 기본값을 null로 설정합니다.

  1. 두 번째는 모델에 입력 필드를 설정합니다.protected $fillable예를 들어 다음과 같습니다.

    protected $fillable = [
        'name', 'email', 'password', 'slug', 'gender','pic'
    ];
    

첫 번째 것보다 두 번째 것이 좋다고 생각합니다.또한 NULL 가능 기능과 Fillable을 동시에 문제없이 설정할 수 있습니다.

또한 Larabel을 사용할 때 이 문제가 발생했지만 별도의 형식에서 정보를 수집할 예정인 테이블에서 "null" 입력을 허용하도록 데이터베이스 스키마를 변경하여 수정했습니다.

public function up()
{

    Schema::create('trip_table', function (Blueprint $table) {
        $table->increments('trip_id')->unsigned();
        $table->time('est_start');
        $table->time('est_end');
        $table->time('act_start')->nullable();
        $table->time('act_end')->nullable();
        $table->date('Trip_Date');
        $table->integer('Starting_Miles')->nullable();
        $table->integer('Ending_Miles')->nullable();
        $table->string('Bus_id')->nullable();
        $table->string('Event');
        $table->string('Desc')->nullable();
        $table->string('Destination');
        $table->string('Departure_location');
        $table->text('Drivers_Comment')->nullable();
        $table->string('Requester')->nullable();
        $table->integer('driver_id')->nullable();
        $table->timestamps();
    });

}

->nullable(); 끝에 추가되었습니다.이건 라라벨을 이용한 거야이게 도움이 됐으면 좋겠네요, 고마워요!

양식 필드/데이터베이스 열을 에 추가하는 을 잊었을 때 이 오류가 발생했습니다.$fillable라라벨 모델에서 어레이를 제거했습니다.

루멘을 하여 이 만, 「루멘」을 「루멘」으로 .DB_STRICT_MODE=false.envfilename을 클릭합니다.

언급URL : https://stackoverflow.com/questions/18751291/mysql-error-1364-field-display-name-doesnt-have-default-value

반응형