Laravel迁移onDelete()查询异常

Laravel Migration onDelete() Query Exception

我有一个简单的 laravel 使用外键的迁移。

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');
    });
}

当我运行迁移命令php artisan migrate:refresh。我收到此错误:

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

通常应该删除所有子项,因为我将 onDelete() 设置为级联。正确的?怎么了?

您设置的cascade选项仅在您删除一行时应用。如果您删除 table 该约束未被触发,这就是您收到错误的原因。

你应该看看 Postgres 文档的 Dependency Tracking 部分