1

SQLSTATE[HY000]: General error: 1005 Can't create table laravel.projects (errno: 150 "Foreign key constraint is incorrectly formed")

I get the above error when I migrate my projects table and try to join three tables which:

  1. A user has many products, and products have their own id.
  2. A product has many projects, and projects have their own id.

User table (user.php)

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->string('email')->unique();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->rememberToken();
        $table->foreignId('current_team_id')->nullable();
        $table->text('profile_photo_path')->nullable();
        $table->timestamps();
    });
}

Product table (product.php)

public function up()
{
    Schema::create('products', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name');
        $table->text('detail');
        $table->string('color');
        $table->string('image');
        $table->string('logo');
        $table->unsignedBigInteger('user_id');
        $table->timestamps();
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
    });
}

Project table (project.php)

Schema::create('projects', function (Blueprint $table) {
    // $table->('id');
    $table->bigIncrements('id');
    $table->string('name', 255)->nullable();
    $table->string('detail', 500)->nullable();
    $table->string('color', 255)->nullable();
    $table->string('image', 22)->nullable();
    $table->string('logo', 22)->nullable();
    $table->unsignedBigInteger('user_id');
    $table->unsignedBigInteger('product_id');
    $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
    $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
    $table->timestamp('created_at')->useCurrent();
    $table->timestamp('updated_at')->nullable();
});

User model

public function getProducts()
{
    return $this->hasMany('App\Models\Product');
}

public function getProject()
{
    return $this->hasMany('App\Models\Project');
}

Product model

use HasFactory;

protected $fillable = [
    'name', 'detail', 'image', 'color', 'logo', 'user_id'
];

public function getUser()
{
    return $this->belongsTo(User::class);
}

Project model

use HasFactory;

protected $fillable = [
    'name', 'detail', 'image','color','logo','user_id'
];

public function getUser(){
    return $this->belongsTo(User::class);
}

I also need help to make my model work.

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
Abdullah Al Shahed
  • 89
  • 2
  • 4
  • 19

3 Answers3

0

For the projects table, Laravel will figure it out itself.

Schema::create('projects', function (Blueprint $table) {
    // $table->('id');
    $table->bigIncrements('id');
    $table->string('name', 255)->nullable();
    $table->string('detail', 500)->nullable();
    $table->string('color', 255)->nullable();
    $table->string('image', 22)->nullable();
    $table->string('logo', 22)->nullable();
    $table->foreignId('user_id')
        ->constrained()
        ->onDelete('cascade');
    
    $table->foreignId('product_id')
        ->constrained()
        ->onDelete('cascade');

    $table->timestamp('created_at')->useCurrent();
    $table->timestamp('updated_at')->nullable();
});
Karl Hill
  • 12,937
  • 5
  • 58
  • 95
sherifcoder
  • 169
  • 7
  • C:\xampp\htdocs\ContentBaseApp\vendor\laravel\framework\src\Illuminate\Database\Connection.php:471 PDOException::("SQLSTATE[HY000]: General error: 1005 Can't create table `laravel`.`projects` (errno: 150 "Foreign key constraint is incorrectly formed")") This is The error now – Abdullah Al Shahed May 30 '21 at 17:28
  • why in your models do you use getUser() , i mean using get makes it an accessor , it is another thing – sherifcoder May 30 '21 at 17:50
  • can you try user() and products() and also projects() , u need an s , this way laravel can understand the relations and guess the tables – sherifcoder May 30 '21 at 17:51
0

The issue is created in Laravel 8+, Usually, all of the answers concern with some older versions, I searched a lot but didn't get the problem of solution for the 8.4x version, And finally I tried different things to fix this issue and now after trying multiple things I find out the solution. Try to change these files Product.php and Project.php file. 100% it will work for you.

Product table (product.php)

  public function up()
   {
    Schema::create('products', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->string('name');
    $table->text('detail');
    $table->string('color');
    $table->string('image');
    $table->string('logo');
    $table->unsignedBigInteger('user_id')->nullable()->index();//Change this line of Code
    $table->timestamps();
    $table->foreign('user_id')->references('id')->on('users')- 
    >onDelete('cascade');
   });
}

Project table (project.php)

Schema::create('projects', function (Blueprint $table) {
   // $table->('id');
   $table->bigIncrements('id');
   $table->string('name', 255)->nullable();
   $table->string('detail', 500)->nullable();
   $table->string('color', 255)->nullable();
   $table->string('image', 22)->nullable();
   $table->string('logo', 22)->nullable();
   $table->unsignedBigInteger('user_id')->nullable()->index(); //Change this line of Code
   $table->unsignedBigInteger('product_id')->nullable()->index();//Change this line of Code
   $table->foreign('user_id')->references('id')->on('users')- 
   >onDelete('cascade');
   $table->foreign('product_id')->references('id')->on('products')- 
   >onDelete('cascade');
   $table->timestamp('created_at')->useCurrent();
   $table->timestamp('updated_at')->nullable();
});

}

0

Your problem is very easy, you need read and learn more about relation query build and table pivot many to many

example project and user

Schema::create('projects_user', function (Blueprint $table) {
    $table->unsignedBigInteger('user_id');
    $table->unsignedBigInteger('product_id');
    $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
    $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
    $table->timestamp('created_at')->useCurrent();
    $table->timestamp('updated_at')->nullable();
});

 php artisan make:migration create_projects_user_table --create=projects_user

is very import search the create tables corrument to use

check is in Spanish please your search in English alternative youtube relation many to many