0
votes

I have a simple laravel migration with a foreign key.

public function up()
{
    Schema::create('smp_posts', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('project_id')->unsigned();
        // Some other stuff
        $table->foreign('project_id')->references('id')->on('smp_projects')->onDelete('cascade');
    });
}

When I run the migrate command php artisan migrate:refresh. I get this error:

Illuminate\Database\QueryException:

SQLSTATE[2BP01]: Dependent objects still exist: 7

ERROR: cannot drop table smp_projects because other objects depend on it DETAIL: constraint smp_posts_project_id_foreign on table smp_posts depends on table smp_projects

Normally all childs should be deleted, because I set the onDelete() to cascade. Right? What's going wrong?

1
Maybe the smp_projects migrations has a date after sms_posts, check the name of the migration. - Andrés Hernández
No smp_posts comes after smp_projects - Markus

1 Answers

1
votes

The cascade option you set applies only when you delete a row. If you drop the table that constraint is not triggered, that's why you get the error.

You should take a look at the Dependency Tracking section of Postgres docs