繁体   English   中英

迁移问题:无法在Laravel中添加外键约束

[英]Migration Problem: Cannot add foreign key constraint in laravel

我试图在Laravel 5.7中创建外键,但是当我使用工匠迁移表时,抛出了以下错误:

Illuminate \\ Database \\ QueryException:

SQLSTATE [HY000]:常规错误:1215无法添加外键约束(SQL:alter table gateway_transactions添加约束gateway_transactions_user_id_foreign外键( user_id )引用删除CASCADE时的usersid

我的迁移:

    public function up()
    {
        Schema::create('gateway_transactions', function (Blueprint $table) {
            $table->engine = "innoDB";
            $table->unsignedBigInteger('id', true);
            $table->integer('user_id')->unsigned();
            $table->enum('provider', \Parsisolution\Gateway\GatewayManager::availableDrivers());
            $table->decimal('amount', 15, 2);
            $table->integer('order_id')->nullable();
            $table->string('currency', 3)->nullable();
            $table->string('ref_id', 100)->nullable();
            $table->string('tracking_code', 50)->nullable();
            $table->string('card_number', 50)->nullable();
            $table->enum('status', \Parsisolution\Gateway\Transaction::availableStates())
                ->default(\Parsisolution\Gateway\Transaction::STATE_INIT);
            $table->string('ip', 20)->nullable();
            $table->json('extra')->nullable();
            $table->timestamp('paid_at')->nullable();
            $table->nullableTimestamps();
            $table->softDeletes();
        });
        Schema::table('gateway_transactions', function(Blueprint $table) {
            $table->foreign('user_id')->references('id')->on('users')->onDelete('CASCADE');
        });
    }

用户迁移:

        Schema::create(config('access.table_names.users'), function (Blueprint $table) {
            $table->increments('id');
            $table->uuid('uuid');
            $table->string('first_name')->nullable();
            $table->string('last_name')->nullable();
            $table->string('email')->unique();
            $table->string('avatar_type')->default('gravatar');
            $table->string('avatar_location')->nullable();
            $table->string('password')->nullable();
            $table->timestamp('password_changed_at')->nullable();
            $table->tinyInteger('active')->default(1)->unsigned();
            $table->string('confirmation_code')->nullable();
            $table->boolean('confirmed')->default(config('access.users.confirm_email') ? false : true);
            $table->string('timezone')->nullable();
            $table->text('National_Code')->nullable();
            $table->char('phone_number', 11)->nullable()->unique();  
            $table->integer('phone_verify')->default(0);
            $table->char('mobile_number', 11)->nullable()->unique();  
            $table->integer('mobile_verify')->default(0);
            $table->text('state')->nullable();
            $table->text('city')->nullable();
            $table->text('address')->nullable();
            $table->text('path')->nullable();
            $table->char('postal_code', 10)->nullable();
            $table->timestamp('last_login_at')->nullable();
            $table->string('last_login_ip')->nullable();
            $table->rememberToken();
            $table->timestamps();
            $table->softDeletes();
        });

为此更改代码:

public function up()
{
    Schema::create('gateway_transactions', function (Blueprint $table) {
        $table->engine = "innoDB";
        $table->unsignedBigInteger('id', true);
        $table->integer('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users')->onDelete('CASCADE');
        $table->enum('provider', \Parsisolution\Gateway\GatewayManager::availableDrivers());
        $table->decimal('amount', 15, 2);
        $table->integer('order_id')->nullable();
        $table->string('currency', 3)->nullable();
        $table->string('ref_id', 100)->nullable();
        $table->string('tracking_code', 50)->nullable();
        $table->string('card_number', 50)->nullable();
        $table->enum('status', \Parsisolution\Gateway\Transaction::availableStates())
            ->default(\Parsisolution\Gateway\Transaction::STATE_INIT);
        $table->string('ip', 20)->nullable();
        $table->json('extra')->nullable();
        $table->timestamp('paid_at')->nullable();
        $table->nullableTimestamps();
        $table->softDeletes();
    });
}

我认为-以下部分是我如何理解Laravel的工作方式。

由于要在第一个模式中创建表,因此必须将外键放入创建中。

问题在于,Laravel Migration将在应用代码之前验证一切正常,但是阶段是函数up()。 因此,其中的所有内容都处于同一阶段。 迁移认为您的gateway_transactions不存在,并且它在验证一切正常时不存在。 该代码将在何时存在。

外键列的类型必须与参考表完全相同。

如果用户表的ID是一个大整数:

$table->bigIncrements('id');

您的外键必须是:

$table->unsignedBigInteger('user_id');

然后,迁移将正常工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM