본문 바로가기
MySql

MySQL Laravel 5.4에서 외래 키 bigInteger를 bigIncrements로 설정

by 베이스 공부 2020. 10. 28.
반응형

그래서 laravel에 대한 마이그레이션 파일에 외래 키를 설정하여 사용자 테이블이 간단하지만 스탠드 증분 대신 bigIncrements를 사용하려고합니다.

 public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->bigIncrements('id')->unsigned();
        $table->string('user_id')->unique();
        $table->string('avatar');
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password')->nullable();
        $table->rememberToken();
        $table->timestampsTz();
    });
}

그리고 다른 테이블을 수행 할 때 외래 키를 추가하려고하면 외래 키가 잘못 형성되었다는 오류가 발생합니다. 나는 열 유형을 일치시키기 때문에 어떻게 혼란스러워합니다. 여기 다른 테이블이 있습니다.

public function up()
{
    Schema::create('social_logins', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->bigIncrements('id');
        $table->bigInteger('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->unsigned()->index();
        $table->string('provider', 32);
        $table->string('provider_id');
        $table->string('token')->nullable();
        $table->string('avatar')->nullable();
        $table->timestamps();
    });
}

 

해결 방법

 

다음에서 unsigned () 제거 :

$table->bigIncrements('id')->unsigned();

그리고 다른 마이그레이션은 다음과 같아야합니다.

$table->bigInteger('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
....
$table->index('user_id');

 

참조 페이지 https://stackoverflow.com/questions/42442498

 

 

반응형

댓글