Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

towhid's avatar

Foreign key constraint is incorrectly formed

this is my user migrations table Schema

Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('email',100)->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });

this is my project migration table Schema , when i using this

Schema::create('projects', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedInteger('owner_id')->index();
            $table->string('title');
            $table->text('description');
            $table->timestamps();
        });

then no error show, but when i am using this line

        Schema::create('projects', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedInteger('owner_id');
            $table->string('title');
            $table->text('description');
            $table->timestamps();

            $table->foreign('owner_id')->references('id')->on('users')->onDelete('cascade');
        });

then show this error

SQLSTATE[HY000]: General error: 1005 Can't create table `laravel-work`.`#sql-2674_366` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `projects` add constraint `projects_owner_id_foreign` foreign key (`owner_id`) references `users` (`id`) on delete cascade)

  at C:\xampp\htdocs\laravel-work\vendor\laravel\framework\src\Illuminate\Database\Connection.php:664
    660|         // If an exception occurs when attempting to run a query, we'll format the error
    661|         // message to include the bindings with SQL, which will make this exception a
    662|         // lot more helpful to the developer instead of just the database's errors.
    663|         catch (Exception $e) {
  > 664|             throw new QueryException(
    665|                 $query, $this->prepareBindings($bindings), $e
    666|             );
    667|         }
    668|

  Exception trace:

  1   PDOException::("SQLSTATE[HY000]: General error: 1005 Can't create table `laravel-work`.`#sql-2674_366` (errno: 150 "Foreign key constraint is incorrectly formed")")
      C:\xampp\htdocs\laravel-work\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458

  2   PDOStatement::execute()
      C:\xampp\htdocs\laravel-work\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458
0 likes
2 replies
dragoneyes96's avatar
Level 1
  1. Are the migrations is in the correct order: Migrate users table first then projects table
  2. Try to change $table->unsignedInteger('owner_id');to $table->unsignedBigInteger('owner_id');
5 likes
towhid's avatar

@DRAGONEYES96 - yes you are right must be follow this order for migration table

  1. user table
  2. then others table
  3. user table data type and foreign table data type are same

thanks

Please or to participate in this conversation.