0

please help for solution this problem //error massage

Illuminate\Database\QueryException:SQLSTATE[HY000]: General error: 1005 Can't create table llc05.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 delet e cascade on update cascade)

//laravel code 
public function up() { 
    Schema::create('posts', function (Blueprint $table) { 
        $table->bigIncrements('id'); 
        $table->unsignedInteger('user_id'); 
        $table->unsignedInteger('catagory_id'); 
        $table->string('title',128); 
        $table->longtext('content'); 
        $table->string('thumbnail_path',128); 
        $table->string('status',32)->default('draft'); 
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade'); 
        $table->foreign('catagory_id')->references('id')->on('catagories')->onDelete('cascade')->onUpdate('cascade'); $table->timestamps(); 
    }); 
}

Dharman
  • 30,962
  • 25
  • 85
  • 135
pksaidur
  • 3
  • 2

1 Answers1

0

Update your migration to this:-

public function up() { 
    Schema::create('posts', function (Blueprint $table) { 
        $table->bigIncrements('id'); 
        $table->unsignedBigInteger('user_id'); 
        $table->unsignedBigInteger('catagory_id'); 
        $table->string('title',128); 
        $table->longtext('content'); 
        $table->string('thumbnail_path',128); 
        $table->string('status',32)->default('draft'); 
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade'); 
        $table->foreign('catagory_id')->references('id')->on('catagories')->onDelete('cascade')->onUpdate('cascade'); $table->timestamps(); 
    }); 
}

Because you are referencing to the bigIncrements so, you need unsignedBigInteger

I hope it'll resolve your problem

Khalid Khan
  • 3,017
  • 1
  • 11
  • 27