【发布时间】:2020-02-20 16:07:33
【问题描述】:
我正在尝试在 Laravel 中进行新的数据库迁移,但它似乎被困在创建外键上。它会产生以下错误。
errno: 150 "Foreign key constraint is incorrectly formed"
我已经找到了这篇文章Laravel migration (errno: 150 "Foreign key constraint is incorrectly formed"),但是我使用了带有 unsignedBigInteger 的 bigIncrements,这样应该可以正常工作吗?而且我在之前的项目中也做了同样的事情,而且效果很好。
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
它似乎停留在活动表中的$table->foreign('game_id')->references('id')->on('games'); 上,这很奇怪,因为它可以很好地处理$table->foreign('user_id')->references('id')->on('users');?当我注释掉 game_id 外键时,它会很好地迁移所有表吗?可能是因为游戏桌尚未创建吗?我将如何解决这个问题?
游戏迁移
<?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');
}
}
广告系列迁移
<?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');
}
}
用户迁移
<?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');
}
}
【问题讨论】: