[英]SQLSTATE[HY000]: General error: 1005 Can't create table `school`.`posts` (errno: 150 "Foreign key constraint is incorrectly formed")
before everything, I tried hard many websites and forums to solve this problem until these posts related to this problem in StackOverflow, but I can't solve the problem.在一切之前,我尝试了许多网站和论坛来解决这个问题,直到这些帖子与 StackOverflow 中的这个问题相关,但我无法解决问题。 I want to create to one to many relationship between Post and Category model but I get that error code.
我想在帖子和类别model 之间创建一对多的关系,但我得到了那个错误代码。
SQLSTATE[HY000]: General error: 1005 Can't create table school
. SQLSTATE[HY000]: 一般错误: 1005 Can't create table
school
。 posts
(errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table posts
add constraint posts_category_id_foreign
foreign key ( category_id
) references categories
( id
) on delete cascade) posts
(errno:150“外键约束不正确”)(SQL:alter table posts
添加约束posts_category_id_foreign
外键( category_id
)在删除级联时引用categories
( id
))
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('slug');
$table->timestamps();
});
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->integer('category_id')->unsigned();
$table->foreign('category_id')->nullable()->references('id')->on('categories');
$table->string('title')->nullable();
$table->timestamps();
});
}
class Category extends Model
{
use HasFactory;
protected $table = 'categories';
protected $guarded = [];
public function posts(){
return $this->hasMany(Post::class);
}
}
class Post extends Model
{
use HasFactory;
protected $guarded = [];
public function category(){
return $this->belongsTo(Category::class);
}
}
please help me I appreciate your help.请帮助我,感谢您的帮助。
Your foreign_key
field and your id
should be of the same type, for example, if categories.id
is bigIncrements
, your foreign_key
inside your Post table
also should be bigInteger
.你的
foreign_key
字段和你的id
应该是相同的类型,例如,如果categories.id
是bigIncrements
,你Post table
的foreign_key
也应该是bigInteger
。
Category Table类别表
Schema::create('categories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('slug');
$table->timestamps();
});
Post Table发布表
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->bigInteger('category_id')->unsigned()->nullable();
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
$table->string('title')->nullable();
$table->timestamps();
});
Note : You should make sure, your Category Table migration is running before Post Table migration.注意:您应该确保您的类别表迁移在发布表迁移之前运行。
$table->id();
creates an unSignedBigInteger
(UNSIGNED BIGINT) .创建一个
unSignedBigInteger
(UNSIGNED BIGINT) 。
$table->integer('category_id')->unsigned();
creates an unSignedInteger
.创建一个
unSignedInteger
。 (UNSIGNED INTEGER) . (无符号整数) 。
// Instead of:
$table->integer('category_id')->unsigned(); ❌
// Use this:
$table->unsignedBigInteger("category_id")->nullable(); ✅
$table->foreign("category_id")
->references("id")
->on("categories")
->onDelete("set null");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.