【问题标题】:Laravel Migration onDelete() Query ExceptionLaravel 迁移 onDelete() 查询异常
【发布时间】:2019-10-05 15:59:36
【问题描述】:

我有一个带有外键的简单 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。我收到此错误:

照亮\数据库\查询异常:

SQLSTATE[2BP01]:依赖对象仍然存在:7

错误:无法删除表 smp_projects,因为其他对象依赖于它 详细信息:表 smp_posts 上的约束 smp_posts_project_id_foreign 取决于表 smp_projects

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

【问题讨论】:

  • 可能 smp_projects 迁移的日期在 sms_posts 之后,请检查迁移的名称。
  • 在 smp_projects 之后没有 smp_posts

标签: php sql laravel postgresql laravel-migrations


【解决方案1】:

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

您应该查看 Postgres 文档的 Dependency Tracking 部分

【讨论】:

    猜你喜欢
    • 2019-09-06
    • 2020-10-06
    • 2018-02-04
    • 2018-04-07
    • 2019-10-22
    • 2013-09-04
    • 2014-04-23
    • 2015-03-25
    • 1970-01-01
    相关资源
    最近更新 更多