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

p0t4t0's avatar

How do I add a foreign key constraint to an already migrated migration?

I want to add a foreign key constraint to my user_id column in my posts table but I'm not sure how I would be able to do that, I read something about doctrine/dbal which can be used to modify columns in your database but it doesn't list anything there about adding foreign key constraints.

I want to add this code to my migration but I'm not sure if the doctrine/dbal ->change() method would work.

$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id');
            $table->string('title');
            $table->text('content');
            $table->string('slug')->nullable();
            $table->string('tags')->nullable();
            $table->timestamps();
        });
    }

Am I just better off creating another migration and just adding the foreign constraint to my posts table?

0 likes
8 replies
topvillas's avatar
Level 46

Yep, create another migration.

But, when you're ready to ship, it's not a bad idea to try and rationalise all your migrations.

p0t4t0's avatar

@topvillas what do you mean by rationalise? I'm sorry, I'm not a native English speaker, my English vocabulary is somewhat minimal.

topvillas's avatar

Look at all the migrations and merge things that should be together.

Like the example you're asking about.

Create a new migration that contains the foreign key but when you're ready to ship, merge it into the posts table migration.

p0t4t0's avatar

@topvillas thanks! one last thing, i'm not quite sure if this creates an actual column or not in my database when i migrate it, so is it still necessary to drop the column on down()?

    public function up()
    {
        Schema::table('posts', function (Blueprint $table) {
            //
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        });
    }

public function down()
    {
        Schema::table('posts', function (Blueprint $table) {
            //
            $table->dropColumn('user_id');
        });
    }
p0t4t0's avatar

@topvillas something went wrong, i am getting this errors when i ran migrate

In Connection.php line 664:

  SQLSTATE[HY000]: General error: 1005 Can't create table `test_db`.`#sql-116c_4d1` (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 delete cascade)


In PDOStatement.php line 107:

  SQLSTATE[HY000]: General error: 1005 Can't create table `test_db`.`#sql-116c_4d1` (errno: 150 "Foreign key constraint is incorrectly formed")


In PDOStatement.php line 105:

  SQLSTATE[HY000]: General error: 1005 Can't create table `test_db`.`#sql-116c_4d1` (errno: 150 "Foreign key constraint is incorrectly formed")
Snapey's avatar

have you posted the same problem 3 times?

Please or to participate in this conversation.