Leo Ribeiro
Leo Ribeiro

Reputation: 1255

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

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

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

        });

Upvotes: 1

Views: 5291

Answers (4)

lakmal_sathyajith
lakmal_sathyajith

Reputation: 325

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.

Upvotes: 0

Amarnasan
Amarnasan

Reputation: 15579

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)

Upvotes: 7

Subash
Subash

Reputation: 7266

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

  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.

Upvotes: 2

Rahul
Rahul

Reputation: 77926

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

both column id and supervisor_id doesn't match datatype and size. Make sure both datatype and size are same for both this column

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

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

Upvotes: 3

Related Questions