jeudi 24 mai 2018

SQLSTATE[HY000]: General error: 1215 - Even with 'InnoDB' and unsigned

I'm trying to create a foreign key on the country_id field from domains table to the id field of countries. But when I do the migration: php artisan migrate:fresh.

Error message:

Illuminate\Database\QueryException  : SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `domains` add constraint `domains_country_id_foreign` foreign key (`country_id`) references `countries` (`id`) on delete cascade)

Here is the domains migration file:

public function up()
{
    Schema::create('domains', function (Blueprint $table) {
        $table->increments('id');
        $table->string('code');
        $table->string('name');
        $table->string('display_name');
        $table->string('extension');
        $table->string('google_analytics')->nullable();
        $table->string('google_webmastertool')->nullable();
        $table->string('google_maps')->nullable();
        $table->integer('country_id')->unsigned();
        $table->boolean('actif')->default(true);
        $table->timestamps();
        $table->engine = 'InnoDB';
    });

    Schema::table('domains', function (Blueprint $table) {
        $table->foreign('country_id')->references('id')->on('countries')->onDelete('cascade');
    });
}

And here is the countries migration file:

public function up()
{
    Schema::create('countries', function (Blueprint $table) {
        $table->increments('id');
        $table->string('code');
        $table->string('name');
        $table->string('alpha2', 2);
        $table->string('alpha3', 3);
        $table->timestamps();
        $table->engine = 'InnoDB';
    });
}

As you can see the country_id field is unsigned and the table engine is InnoDB. I have already done other migrations with foreign key and they work fine, but this one, doesn't work :|

Thanks in advance for your help!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire