samedi 5 mars 2016

cannot add foreign key constraint Laravel 5.1

I first created all the tables without their foreign keys, I then added the foreign keys for each table, starting with the first table, i got this error:

SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL
  : alter table `accounts` add constraint accounts_client_id_foreign foreign
  key (`client_id`) references `clients` (`id`))

Here's my code:

public function up()
    {
        Schema::create('accounts', function(Blueprint $table)
        {
        $table->engine = 'InnoDB';

        $table->bigInteger('id');

        $table->integer('client_id')->unsigned();
        $table->foreign('client_id')->references('id')->on('clients');

        $table->integer('emp_id')->unsigned();
        $table->foreign('emp_id')->references('id')->on('employees');

        $table->string('type');

        $table->timestamps();
    });
}

I tried without $table->engine='innoDB'; but same error

Plus, i tried to separate the foreign keys:

Schema::table('accounts', function($table) {
        $table->foreign('client_id')->references('id')->on('clients');
        $table->foreign('emp_id')->references('id')->on('employees');
    });

I got this error:

Base table or view already exists: 1050 Table 'accounts' already exists

So when I deleted and re-migrate i get the first error

So whats happening?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire