php - Laravel 5.1 - 一般错误 : 1005 Can't create table (mysql)

标签 php mysql laravel laravel-5.1

我正在使用 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`))  

我尝试了很多东西,但都没有用。

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

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

        });

最佳答案

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

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

关于php - Laravel 5.1 - 一般错误 : 1005 Can't create table (mysql),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32397645/

相关文章:

php - Eloquent 关系 - 查询行到列

javascript - 使用 jquery 选中或取消选中父父复选框

php - MySQL 表结果处理另一个表结果

mysql - 将复杂的查询循环转化为单个查询

Laravel 5 每次销售产品数量减少

php - PHP/mySQL 无法插入表

PHP : multiple inputs into other multiple inputs

mysql - SQL更新两个表事务语法

php - 如何将 MYSQL 查询转移到 Laravel Eloquent 子查询?

php - 一般错误 : 1 no such table: users in Laravel Unit Test