2017-12-19 11 views
2

Laravel, PHP 및 MySql에 대한 지식이 제한되어 있습니다. 외래 키를 사용하여 3 개의 연결된 테이블이있는 데이터베이스를 설정하려고합니다. 이전에 WorkBench MySql에서이 작업을 수동으로 수행했지만 Laravel에서는이 작업을 수행 할 수 없습니다. 여기에 내 오류와 내 코드가 있습니다.Laravel 마이그레이션에서 3 개의 테이블을 연결하는 방법 (구문 오류 또는 액세스 위반)

SQL STATE[42000]: Syntax error or access violation: 1072 Key column 'cat_id 
    ' doesn't exist in table (SQL: alter table `users` add index `users_cat_id 
    _usr_id_index`(`cat_id`, `usr_id`)) 

표 1

public function up() 
{ 
    Schema::create('users', function (Blueprint $table) { 
     $table->increments('usr_id')->autoIncrement(); 
     $table->string('usr_email',45)->unique(); 
     $table->string('usr_password',256); 
     $table->rememberToken(); 
     $table->string('usr_name',45); 
     $table->string('usr_surname',45); 
     $table->string('usr_title',45); 
     $table->string('usr_psc',5); 
     $table->string('usr_region',128); 
     $table->string('usr_adress',128); 
     $table->string('usr_tel',10); 
     $table->string('usr_tel_int',13); 
     $table->binary('usr_image'); 
     $table->tinyInteger('usr_type'); 
     $table->index(['cat_id','usr_id'])->nullable(); 
     $table->index(['tem_id','usr_id'])->nullable(); 

    }); 
} 

표 2

public function up() 
{ 
    Schema::create('category', function (Blueprint $table) { 
     $table->primary('cat_id'); 
     $table->string('cat_name',45); 
    }); 
    Schema::table('category', function(Blueprint $table) { 
     $table->foreign('cat_id')->references('cat_id')->on('users'); 
     }); 
} 

표 3

public function up() 
{ 
    Schema::create('team', function (Blueprint $table) { 
     $table->increments('tem_id'); 
     $table->unique('tem_sub_id'); 
     $table->string('tem_name',45); 
     $table->string('tem_subteam_name',45); 

    }); 
    Schema::table('team', function(Blueprint $table) { 
     $table->foreign('tem_id')->references('tem_id')->on('users'); 
     }); 

} 
+0

올바르게 마이그레이션을 만드는 문제가 있다면 난 당신이 내가 전에이 작업을 시도 migraion 파일 – DestinatioN

답변

1

cat_idusers 테이블에 존재하지 않기 때문에 오류가 발생합니다. 그 외에도, Laravel 마이그레이션 작성할 때 지켜야 할 몇 가지 사항이 있습니다 :

  • 당신이 FOREIGN_KEY_CHECKS to 0을 설정하지 않는 한, 당신은 단지 기존 테이블에 참조 할 수 있습니다 내가 당신의 테이블을 만드는 순서를 변경하는 이유, 즉 내 아래의 예를 참조하십시오.

  • Laravel 마법 방법을 작동 시키려면 테이블 이름을 복수형 (사용자, 팀, 범주)으로 작성해야합니다.

  • 'usr_id'대신 'id'를 사용하십시오. 데이터베이스 컬럼의 접 두부로 테이블 이름을 사용하지 않도록하십시오.

Laravel 웅변 ORM은 크게 표준에 의존하기 때문에, 나는 아래처럼 패션에 테이블을 재 작성하는 것이 좋습니다 것

, 다음 Laravel Eloquent conventions :

표 범주

public function up() 
{ 
    Schema::create('categories', function (Blueprint $table) { 
     $table->increments('id'); 

     $table->string('name',45); 
     // etc... 
    }); 

} 

테이블 팀

표 사용자

public function up() 
{ 
    Schema::create('users', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->integer('category_id')->unsigned()->nullable();; 
     $table->foreign('category_id')->references('id')->on('categories'); 
     $table->integer('team_id')->unsigned()->nullable(); 
     $table->foreign('team_id')->references('id')->on('teams'); 

     $table->string('email',45)->unique(); 
     // etc... 

    }); 
} 
+0

을 생성하는 온라인 도구입니다 http://laravelsd.com/를 사용하는 것이 좋습니다 내 첫 번째 게시물. 다음은 코드 수정 후의 오류입니다. SQLSTATE [42000] : 구문 오류 또는 액세스 위반 : 1072 키 열 'cat_id'가 테이블에 없습니다 (SQL : alter table 사용자가 인덱스 users_cat_id _index (cat_id)를 추가 함) – SnatchIT

+0

@SnatchIT, 내 대답을 업데이트했습니다. 희망이 도움이됩니다. 문제/질문에 초점을 맞추기 위해 명확성을 위해 일부 코드를 생략했습니다. – iep

+0

내 코드가 변경되었지만 아직 Laravel에 데이터베이스가 없습니다. SQLSTATE [HY000] : 일반 오류 : 1215 외래 키 제약 조건을 추가 할 수 없습니다 (SQ L : 테이블 변경'사용자'제약 조건'외래 키 ('cat_id') 참조'category' ('id')) 접속 중 .PHP 라인 458 : SQLSTATE [HY000] : 일반 오류 : 1215 외래 키 제약 조건을 추가 할 수 없습니다 – SnatchIT