简体   繁体   English

SQLSTATE[HY000]: 一般错误: 1005 Can't create table `school`.`posts` (errno: 150 "Foreign key constraint is wrongly forms")

[英]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 )在删除级联时引用categoriesid ))

        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.idbigIncrements ,你Post tableforeign_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) . (无符号整数)

Fix使固定

// 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");

Delete Foreign Key Contsraints From bottom of your table从表格底部删除外键约束

Please see a screenshot here在此处查看屏幕截图

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQLSTATE[HY000]: General error: 1005 Can't create table `business`.`users` (errno: 150 "Foreign key constraint is incorrectly formed") Laravel 7 - SQLSTATE[HY000]: General error: 1005 Can't create table `business`.`users` (errno: 150 "Foreign key constraint is incorrectly formed") Laravel 7 Laravel 5.4:SQLSTATE[HY000]:一般错误:1005 无法创建表“外键约束格式不正确” - Laravel 5.4: SQLSTATE[HY000]: General error: 1005 Can't create table "Foreign key constraint is incorrectly formed" 一般错误:1005 无法创建表(错误号:150“外键约束格式不正确”) - General error: 1005 Can't create table (errno: 150 "Foreign key constraint is incorrectly formed") 常规错误:1005无法创建表errno:150“外键约束格式错误”) - General error: 1005 Can't create table errno: 150 “Foreign key constraint is incorrectly formed”) 一般错误:1005 无法创建表...(errno:150“外键约束格式不正确” - General error : 1005 Can't create table ... (errno:150 "Foreign key constraint is incorrectly formed" Laravel 一般错误:1005 无法创建表`categories_products`(错误号:150“外键约束的格式不正确”) - Laravel General error: 1005 Can't create table `categories_products` (errno: 150 "Foreign key constraint is incorrectly formed") 错误1005(HY000):无法创建表“ db.POSTS”(错误号:150) - ERROR 1005 (HY000): Can't create table 'db.POSTS' (errno: 150) : 1005 无法创建表 `shop`.`role_user` (errno: 150 “外键约束格式不正确”)") - : 1005 Can't create table `shop`.`role_user` (errno: 150 “Foreign key constraint is incorrectly formed”)") SQLSTATE[HY000]:一般错误:1005 无法创建表 Laravel 8 - SQLSTATE[HY000]: General error: 1005 Can't create table Laravel 8 SQLSTATE [HY000]:一般错误:1005 无法创建表 - Laravel 4 - SQLSTATE[HY000]: General error: 1005 Can't create table - Laravel 4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM