I am trying to do a fresh database migration in Laravel but it seems to be stuck on creating a foreign key. It generates the following error.
errno: 150 "Foreign key constraint is incorrectly formed"
I found this post already Laravel migration (errno: 150 "Foreign key constraint is incorrectly formed") but I used bigIncrements with unsignedBigInteger so that should work right? And I also did exactly the same in a previous project and that worked just fine.
Dropped all tables successfully.
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_000000_create_users_table (0.42 seconds)
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated: 2014_10_12_100000_create_password_resets_table (1.05 seconds)
Migrating: 2019_08_19_000000_create_failed_jobs_table
Migrated: 2019_08_19_000000_create_failed_jobs_table (0.65 seconds)
Migrating: 2019_10_23_110756_create_campaigns_table
It seems to be stuck on $table->foreign('game_id')->references('id')->on('games');
in the campaigns table, which is wierd because it does $table->foreign('user_id')->references('id')->on('users');
just fine? When I comment out the game_id foreign key it migrates all the tables just fine? Is it maybe because the games table has not been created? And how would I solve this?
Games Migration
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateGamesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('games', function (Blueprint $table) {
$table->bigIncrements('id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('games');
}
}
Campaign Migration
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateCampaignsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('campaigns', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->longText('settings');
$table->longText('preview_settings');
$table->text("description")->nullable();
$table->enum('type', ['desktop', 'appstore', 'facebook', 'messenger', 'playstore', 'mobile']);
$table->enum('published', ['Draft', 'Ready', 'Live']);
$table->datetime("start_time")->nullable();
$table->datetime("end_time")->nullable();
$table->timestamps();
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('game_id');
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('game_id')->references('id')->on('games');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('campaigns');
}
}
User Migration
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}