【问题标题】:Why is my foreign key not working when migrating - Laravel为什么我的外键在迁移时不起作用 - Laravel
【发布时间】: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');
    }
}

【问题讨论】:

    标签: laravel migration


    【解决方案1】:

    因为活动表已创建,但游戏表尚未创建。

    迁移按顺序进行。

    查看列表,游戏迁移不显示;

    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
    

    你能用这种方式编辑和尝试代码吗?

    游戏迁移;

    <?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();
            });
    
        Schema::table('campaigns', function(Blueprint $table)
        {
            $table->foreign('game_id')->references('id')->on('games');
        });
    
        }
    
        /**
         * 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');
            });
        }
    
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::dropIfExists('campaigns');
        }
    }
    

    【讨论】:

    • 这真的是一种可读的修复方法吗?是否可以通过让我为广告系列拥有单独的迁移文件的方式来完成?
    • 您可以先设计数据库模式。这样会更准确。
    【解决方案2】:
    $table->bigInteger('user_id')->unsigned();
    $table->bigInteger('game_id')->unsigned();
    

    【讨论】:

      猜你喜欢
      • 2021-02-08
      • 2021-06-09
      • 2019-08-22
      • 2021-04-21
      • 2019-11-23
      • 1970-01-01
      • 2017-10-08
      • 2011-03-18
      • 1970-01-01
      相关资源
      最近更新 更多