【发布时间】:2021-07-27 13:08:16
【问题描述】:
在我的 Laravel-8 应用程序中,我进行了以下迁移:
public function up()
{
Schema::create('profiles', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('user_id');
$table->string('first_name',50);
$table->string('last_name',50);
$table->string('other_name',50)->nullable();
$table->string('gender',20);
$table->string('user_photo',350)->nullable();
$table->integer('nationality_id');
$table->string('marital_status',50);
$table->date('dob')->nullable();
$table->string('address', 300)->nullable();
$table->integer('country_id')->nullable();
$table->integer('state_id')->nullable();
$table->integer('city_id')->nullable();
$table->string('cv_file',350)->nullable();
$table->text('summary')->nullable();
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('nationality_id')->references('id')->on('countries');
$table->foreign('country_id')->references('id')->on('countries');
$table->foreign('state_id')->references('id')->on('state_origins');
$table->foreign('city_id')->references('id')->on('cities');
});
}
当我做 php artisan migrate 时,我得到了这个错误:
SQLSTATE[HY000]: 一般错误: 1005 Can't create table
hrm.profiles(errno: 150 "外键约束格式不正确") (SQL: alter tableprofilesadd constraintprofiles_user_id_foreign外键(user_id)引用users(id))
这是用户表:
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name', 100)->nullable();
$table->string('email', 80)->nullable();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->string('avatar', 200)->nullable();
$table->boolean('active')->default(false);
$table->string('activation_token', 250)->nullable();
$table->rememberToken();
$table->timestamps();
$table->softDeletes()->nullable();
$table->timestamp('last_login_at')->nullable();
});
}
我该如何解决这个问题?
谢谢
【问题讨论】:
-
所有外键都需要无符号,将
bigInteger更改为unsignedBigInteger,primary和foreign references必须是同一类型。bigIncrements()需要unsignedBigInteger()和increments()需要unsignedInteger() -
嗯,我认为你的 mysql 服务器有问题。我在我的 wampserver 中运行了您的代码,并且创建的表没有任何错误。
标签: laravel