2
votes

Ok I'm stuck. Seems like a simple thing and I've seen others have issues as well. I'm using the latest version 5.8.x. I have no issues creating the tables but when I start with a fresh database and add the foreign keys I get error-

Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1005 Can't create table apbase.#sql-546_1a14 (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table apconfig add constraint apconfig_apevent_id_foreign foreign key (apevent_id) references apevents (id))

I have renamed migration files so any nitration with a foreign key is migrated after the reference table. The migration files were created using artisan.

Schema::create('apevents', function(Blueprint $table)
        {
            $table->bigIncrements('id');
            $table->string('name', 200);
            $table->timestamps();
        });
Schema::create('apconfig', function(Blueprint $table)
    {
        $table->bigIncrements('id');
        $table->bigInteger('apevent_id');
        $table->foreign('apevent_id')->references('id')->on('apevents');
        $table->string('name', 200);
        $table->timestamps();
    });
1

1 Answers

4
votes

your apevent_id in apconfig table should be

$table->unsignedBigInteger('apevent_id');