2015-09-04 60 views
1

我使用laravel遷移一些數據,但我有下面這條消息:Laravel 5.1 - 常規錯誤:1005無法創建表(MySQL的)

[Illuminate\Database\QueryException]                                         
    SQLSTATE[HY000]: General error: 1005 Can't create table 'heinz.#sql-1e83_8' (errno: 150) (SQL: alter table `funcionarios` add constraint funcionarios_supervisor_id_foreign foreign key (`supervis 
    or_id`) references `funcionarios` (`id`)) 

我嘗試了很多東西,但沒有工作。

這是遷移文件的代碼。 (相關部分)。

Schema::create('funcionarios', function (Blueprint $table) { 

//   $table->engine = 'InnoDB'; 

      $table->increments('id'); 
      $table->string('nome', 60); 
      $table->integer('matricula')->unsigned()->unique(); 
      $table->bigInteger('pis_pasep')->unsigned()->nullable(); 
      $table->date('data_admissao')->nullable(); 
      $table->date('data_demissao')->nullable()->default(null); 
      $table->date('data_nascimento')->nullable(); 
      $table->string('apelido', 20)->nullable(); 
      $table->integer('supervisor_id')->nullable(); 
      $table->integer('coordenador_id')->nullable(); 
      $table->integer('gerente_id')->nullable(); 
      $table->integer('diretor_id')->nullable(); 
      $table->integer('sexo_id')->nullable(); 
      $table->integer('setor_id')->nullable(); 
      $table->integer('cargo_id'); 
      $table->integer('turno_id')->nullable(); 
      $table->timestamps(); 
     }); 

     Schema::table('funcionarios', function($table){ 

      $table->foreign('supervisor_id')->references('id')->on('funcionarios'); 
      $table->foreign('coordenador_id')->references('id')->on('funcionarios'); 
      $table->foreign('gerente_id')->references('id')->on('funcionarios'); 
      $table->foreign('diretor_id')->references('id')->on('funcionarios'); 
      $table->foreign('sexo_id')->references('id')->on('sexos'); 
      $table->foreign('setor_id')->references('id')->on('setores'); 
      $table->foreign('cargo_id')->references('id')->on('cargos'); 
      $table->foreign('turno_id')->references('id')->on('turnos'); 

     }); 

回答

7

您所有的外鍵的必須是無符號

$table->integer('supervisor_id')->unsigned()->nullable(); 
    $table->integer('coordenador_id')->unsigned()->nullable(); 
    $table->integer('gerente_id')->unsigned()->nullable(); 
    $table->integer('diretor_id')->unsigned()->nullable(); 
    $table->integer('sexo_id')->unsigned()->nullable(); 
    $table->integer('setor_id')->unsigned()->nullable(); 
    $table->integer('cargo_id')->unsigned(); 
    $table->integer('turno_id')->unsigned()->nullable(); 

(來源:http://laravel.com/docs/4.2/schema#foreign-keys)需要

+0

此處同樣的錯誤。 –

+0

不用等。我只是忘了一個鍵來做無符號的。有效! –

+0

Amarnasan,你怎麼知道外表中的密鑰是未簽名的? – silkfire

3

沒有看到從下面的查詢表結構,這可能是因爲

都列idsupervisor_id不匹配的數據類型和大小。確保這兩列的數據類型和大小都相同

另外,檢查名稱爲funcionarios_supervisor_id_foreign的任何其他約束是否已存在。如果是這樣,請嘗試爲約束提供不同的名稱。

alter table `funcionarios` add constraint funcionarios_supervisor_id_foreign 
foreign key (`supervisor_id`) references `funcionarios` (`id`) 
+0

'supervisor_id'引用同一個表'funcionarios',並且它們都是相同的類型。並不存在具有相同名稱的另一個約束。 –

1

當代碼中存在錯誤的主鍵引用時,您會得到error code 1005。這裏是你可以做什麼來調試你的代碼:

  1. 確保你所指的FK實際存在。
  2. 請確保您沒有錯別字。案件也必須相同。
  3. FK鏈接的字段必須完全匹配定義。
+0

FK引用同一張表'funcionarios',所以它確實存在。顯然,一切都是匹配的。但錯誤不斷出現。 –

0

遷移文件的執行順序來首先檢查。引用的表遷移應在完整性constains中引用之前完成。

相關問題