2015-09-04 57 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中引用之前完成。

相关问题