Laravel 迁移错误 外键约束格式不正确
这是每次显示的错误。
Illuminate\Database\QueryException
SQLSTATE[HY000]: General error: 1005 Can't create table `codehacking`.`posts` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `posts` add constraint `posts_user_id_foreign` foreign key (`user_id`) references `users` (`id`) on delete cascade)
1 G:\Laravel\CodeHacking\vendor\laravel\framework\src\Illuminate\Database\Connection.php:501
PDOException::("SQLSTATE[HY000]: General error: 1005 Can't create table `codehacking`.`posts` (errno: 150 "Foreign key constraint is incorrectly formed"))
2 G:\Laravel\CodeHacking\vendor\laravel\framework\src\Illuminate\Database\Connection.php:501
PDOStatement::execute()
用户表
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->integer('role_id')->index()->unsigned()->nullable();
$table->integer('is_active')->default(0);
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
帖子表
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->integer('user_id')->unsigned()->index();
$table->integer('category_id')->unsigned()->index();
$table->integer('photo_id')->unsigned()->index();
$table->string('title');
$table->string('body');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}
我每次尝试迁移表时都会出现此错误。如何解决它? 该代码基于Laravel-9。每当我遇到此错误时,我都在尝试使用PHP Aritsan迁移命令创建表。
This is the error showing every time.
Illuminate\Database\QueryException
SQLSTATE[HY000]: General error: 1005 Can't create table `codehacking`.`posts` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `posts` add constraint `posts_user_id_foreign` foreign key (`user_id`) references `users` (`id`) on delete cascade)
1 G:\Laravel\CodeHacking\vendor\laravel\framework\src\Illuminate\Database\Connection.php:501
PDOException::("SQLSTATE[HY000]: General error: 1005 Can't create table `codehacking`.`posts` (errno: 150 "Foreign key constraint is incorrectly formed"))
2 G:\Laravel\CodeHacking\vendor\laravel\framework\src\Illuminate\Database\Connection.php:501
PDOStatement::execute()
users table
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->integer('role_id')->index()->unsigned()->nullable();
$table->integer('is_active')->default(0);
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
posts table
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->integer('user_id')->unsigned()->index();
$table->integer('category_id')->unsigned()->index();
$table->integer('photo_id')->unsigned()->index();
$table->string('title');
$table->string('body');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}
I am having this error every time I try to migrate my table. How to solve it.?
This code is based on laravel-9. I am trying to create tables with the php aritsan migrate command every time I am getting this error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我也有这个错误。当外键类型及其参考类型与不匹配时,此错误将处理。例如,如果外键类型为(int),但是参考密钥类型为(biginteger)此错误将处理。
I had this error too . This error will handle when foreign key type and its reference type are not match . For example , if foreign key type is (int) but reference key type is (biginteger) this error will handle.