简体   繁体   中英

Laravel Migration onDelete() Query Exception

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM