错误:150在Laravel迁移中“外键约束的格式不正确”

时间:2019-03-22 15:07:15

标签: php laravel laravel-5

我正在使用Laravel 5.7版本。运行命令users_activations

时,出现php artisan migrate表的以下错误
  

SQLSTATE [HY000]:常规错误:1005无法创建表auf#sql-1ecc_fa(错误号:150“外键约束格式不正确”)(SQL:更改表{{1} }在删除级联上添加约束users_activations外键(users_activations_user_id_foreign)引用user_idusers

id

users_activations

Schema::create('users_activations', function (Blueprint $table) { $table->bigIncrements('id'); $table->integer('user_id')->unsigned(); $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); // got error for this line $table->string('token'); $table->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP')); });

user

我已经在搜索错误了。

我已经改变

Schema::create('users', function (Blueprint $table) { $table->bigIncrements('id'); $table->integer('role_id')->default('1'); $table->string('name'); $table->string('email')->unique(); $table->timestamp('email_verified_at')->nullable(); $table->string('password'); $table->rememberToken(); $table->timestamps(); });

$table->integer('user_id')->unsigned();表中的

users_activations。但是没用。

有人帮我吗?

预先感谢

1 个答案:

答案 0 :(得分:1)

外键应与id相同,将外键更改为bigInteger:

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