2017-09-19 1 views
0

우선,이 오류를 이미 찾고 솔루션을 제공하지 않았습니다. 내 사용자 테이블 임상 테이블의 clinic_id foranea 키에 추가 왔지만 나는 다음과 같은 errror 얻을 마이그레이션 할 때 :외래 키 오류, '기본 테이블 또는 뷰가 이미 존재합니다.'laravel5

[Illuminate\Database\QueryException] 
    SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists (SQL: create table `users` (`id` int unsigned not null auto_increment primary key, `name` varc 
    har(150) not null, `surnames` varchar(150) not null, `email` varchar(150) not null, `password` varchar(150) not null, `phone` int not null, `clinic_id` int not null, `remember_toke 
    n` varchar(100) null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate utf8mb4_unicode_ci) 


    [PDOException] 
    SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists 

2014_10_12_000000_create_clinics_table.php :

public function up() 
{ 

    Schema::create('clinics', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->string('name'); 
     $table->string('country'); 
     $table->string('province'); 
     $table->timestamps(); 
    }); 
} 

을 2014_10_12_000001_create_users_table.php :

public function up() 
{ 


    Schema::create('users', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->string('name'); 
     $table->string('surnames'); 
     $table->string('email')->unique(); 
     $table->string('password'); 
     $table->integer('phone'); 
     $table->integer('clinic_id'); 
     $table->foreign('clinic_id')->references('id')->on('clinics'); 
     $table->rememberToken(); 
     $table->timestamps(); 
    }); 
} 

이전 프로젝트 버전으로 돌아 왔는데 그 오류가 다시 발생하지 않았으므로 지금은 분명히 모든 것이 올바르지 만 여전히 테이블 사용자에게 외부 필드를 추가하지는 않습니다. :

public function up() 
{ 
    Schema::create('users', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->string('name'); 
     $table->string('surnames'); 
     $table->string('email')->unique(); 
     $table->string('password'); 
     $table->integer('clinic_id'); 
     $table->foreign('clinic_id')->references('id')->on('clinics'); 
     $table->rememberToken(); 
     $table->timestamps(); 
    }); 

} 

등.

public function up() 
    { 
     Schema::create('users', function (Blueprint $table) { 
      $table->increments('id'); 
      $table->string('name'); 
      $table->string('surnames'); 
      $table->string('email')->unique(); 
      $table->string('password'); 

      $table->rememberToken(); 
      $table->timestamps(); 
     }); 
     Schema::table('users', function (Blueprint $table) { 
     $table->integer('clinic_id'); 
     $table->foreign('clinic_id')->references('id')->on('clinics'); 
    }); 
    } 

답변

1

새 테이블을 만들지 않고 기존 테이블에 추가하려고합니다. 그래서 당신은 당신이 추가하는 열만을 나열해야하고, 이미 존재하는 테이블을 만들려고 꽤 명확하고 실행 해 보인다 대신 create

public function up() 
{ 
    Schema::table('users', function (Blueprint $table) { 
     $table->integer('clinic_id'); 
     $table->foreign('clinic_id')->references('id')->on('clinics'); 
    }); 
} 
+0

테이블을 삭제하고 다시 마이그레이션을 수행하여 동일한 오류가 발생합니다 ... – jlgf

+0

사용자 테이블에 대해 여러 마이그레이션이 있습니까? – aynber

+0

사용자에게 단 하나의 마이그레이션이 있습니다. 매우 희귀합니다. – jlgf

0

메시지의 table를 사용

php artisan migrate:rollback 

그래도 작동하지 않으면 롤백 할 수 있지만 테이블을 놓치지 않은 경우 수동으로 놓으십시오. 잘못하고있는 열을 추가하려고하면 기존 테이블을 호출하고 새 열을 첨부해야합니다.

Schema::table('users', function (Blueprint $table) { 
     $table->integer('clinic_id'); 
     ... 
    }); 

편집 : 당신이 사용하기 때문에 당신은 당신이, 당신이 한 경우, users라는 새 테이블을 만들려고하면 발행 users 테이블 마이그레이션입니다 이미 한 다음 인증 시스템을 구축하기 위해 php artisan make:auth를 사용한 경우 다음

Schema::create('users', function (Blueprint $table) { 
     });//This is just for creating a new users table 

당신이 다음 다음 마이그레이션해야 할 사용자 테이블에 새 열을 만들고 싶었 경우

Schema::table('users', function (Blueprint $table) { 
    });// See the difference?, create/table 

복잡하지 않으려면 방금 생성 한 사용자 모델의 마이그레이션을 삭제하고 원래 users 이전에 열을 추가하면됩니다.

+0

테이블을 삭제하고 다시 마이그레이션을 수행하여 같은 오류가 발생합니다 ... – jlgf

+0

자, 사용자 테이블을 두 번 마이그레이션했다고 가정합니다. 첫 번째 마이그레이션에서 필요한 열을 추가하고 새 열을 삭제하거나 첫 번째 마이그레이션을 유지하고 새 열을 추가하기 만하면 새 열을 추가 할 수 있습니다. Schema :: create는 테이블을 만들고 Schema :: table은 이미 만들어진 테이블을 가져 와서 원하는대로 편집합니다. –

+0

사용자에게 단 하나의 마이그레이션이 있습니다 ... 매우 드물습니다. – jlgf