Laravel 5.1 - 一般错误:1005 无法创建表 (mysql)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32397645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:17:58  来源:igfitidea点击:

Laravel 5.1 - General error: 1005 Can't create table (mysql)

phpmysqllaravellaravel-5.1

提问by Leo Ribeiro

I'm using laravel to migrate some data, but i'm having this message below:

我正在使用 laravel 迁移一些数据,但我在下面收到了这条消息:

[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`))  

I tried a lot of thing, but didn't work.

我尝试了很多东西,但没有奏效。

Here is the code of the migration file. (the relevant part).

这是迁移文件的代码。(相关部分)。

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');

        });

回答by Amarnasan

All of your foreign keys must be unsigned

你所有的外键都必须是未签名的

    $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();

(source: http://laravel.com/docs/4.2/schema#foreign-keys)

(来源:http: //laravel.com/docs/4.2/schema#foreign-keys

回答by Rahul

Without seeing your table structure, from your below query it could be that

没有看到你的表结构,从你下面的查询可能是

both column idand supervisor_iddoesn't match datatype and size. Make sure both datatype and size are same for both this column

idsupervisor_id不匹配数据类型和大小。确保此列的数据类型和大小相同

Also, check if any other constraint with name funcionarios_supervisor_id_foreignalready exists. If so, try providing a different name for the constraint.

此外,检查是否funcionarios_supervisor_id_foreign已存在任何其他具有名称的约束。如果是这样,请尝试为约束提供不同的名称。

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

回答by Subash

You get error code 1005when there is a wrong primary key reference in your code. Here is what you can do to debug your code:

error code 1005当您的代码中存在错误的主键引用时,您会得到。您可以执行以下操作来调试代码:

  1. Make sure that your FK you are referring actually exists.
  2. Make sure that you don't have typo. The case must be same too.
  3. FK-linked fields must match definitions exactly.
  1. 确保您所指的 FK 确实存在。
  2. 确保你没有错别字。情况也必须相同。
  3. FK 链接的字段必须与定义完全匹配。

回答by lakmal_sathyajith

The execution order of the migration files needs to be checked first. the referenced table migration should be done before refer it in integrity constains.

首先需要检查迁移文件的执行顺序。被引用的表迁移应该在将它引用到完整性constains 之前完成。