Laravel 5.8: General error: 1005 Can't create table

Laravel 5.8: General error: 1005 Can't create table

我正在与 Laravel 5.8 一起开发我的项目,我只是创建了一个 Migration,如下所示:

public function up()
    {
        Schema::create('user_wallet_transactions', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedSmallInteger('user_id');
            $table->foreign('user_id')->references('usr_id')->on('users');
            $table->unsignedSmallInteger('wallet_id');
            $table->foreign('wallet_id')->references('id')->on('wallets');
            $table->string('amount');
            $table->string('description');
            $table->timestamps();
        });
    }

但是当我 运行 这个时,我得到这个错误:

SQLSTATE[HY000]: General error: 1005 Can't create table `nanonew_main`.`user_wallet_transactions` (errno: 150 "Foreign key
 constraint is incorrectly formed") (SQL: alter table `user_wallet_transactions` add constraint `user_wallet_transactions_user_id_foreign` foreign key (`user_id`)
references `users` (`usr_id`))

我不知道这里出了什么问题!我的 table users 是这样的,字段名为 usr_id:

public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('usr_id');
            $table->string('usr_name')->unique();
            $table->boolean('usr_is_admin')->default(0)->comment('0 = admin , 1 = not admin');
            $table->boolean('usr_is_active')->default(0);
            $table->string('usr_email')->unique()->nullable();
            $table->timestamp('usr_email_verified_at')->nullable();
            $table->string('usr_password');
            $table->rememberToken();
            $table->timestamps();

            $table->engine = 'InnoDB';
        });
    }

和table wallets也有字段名为id:

public function up()
    {
        Schema::create('wallets', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('title');
            $table->string('name')->unique();
            $table->tinyInteger('is_active');
            $table->tinyInteger('is_cashable');
            $table->timestamps();
        });
    }

那么这里的问题是什么?我该如何解决?

非常感谢你们的任何想法或建议...

提前致谢。

您正在使用 bigIncrements 作为主键,因此请将您的外键也设为相同类型,更改为:

$table->unsignedSmallInteger('user_id');
$table->unsignedSmallInteger('wallet_id');

$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('wallet_id');

或者

$table->bigInteger('user_id')->unsigned();
$table->bigInteger('wallet_id')->unsigned();