Level 1
- Are the migrations is in the correct order: Migrate users table first then projects table
- Try to change
$table->unsignedInteger('owner_id');to$table->unsignedBigInteger('owner_id');
5 likes
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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
$table->unsignedInteger('owner_id');to $table->unsignedBigInteger('owner_id');
Please or to participate in this conversation.