Migration Problem: Cannot add foreign key constraint in laravel

Posted 3 months ago by mDelshad

i'm install a package And I want to make changes to the package migration

I'm trying to create foreign keys however when I migrate package table using artisan i am thrown the following error:

    Illuminate\Database\QueryException:

    SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table gateway_transactions add constraint gateway_transactions_user_id_foreign foreign key (user_id) references users (id) on delete CASCADE)

package migration :

        Schema::create($this->getTable(), function (Blueprint $table) {
           // $table->engine = "innoDB";
            $table->unsignedBigInteger('id', true);
            $table->unsignedInteger('user_id');
        $table->foreign('user_id')->references('id')->on('users');
            $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();
        });

and users migration :

    public function up()
    {
        Schema::create('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();
        });
    }

Please sign in or create an account to participate in this conversation.