php - 删除外键的问题

标签 php mysql laravel laravel-artisan

我的外键与其自己的表相关。这是为了生成具有层次结构的帖子

现在,当我尝试将列删除到数据库中时,出现以下错误:

1553 - Cannot drop index 'post_field_properties_parent_id_index': needed in a foreign key constraint

这是代码:

public function down()
{
        Schema::table( "post_field_properties", function( $table )
        {
            $table->dropForeign('parent_id');
            $table->dropColumn('parent_id');
        } );
}

我似乎能够做到的唯一方法是转到 phpmyadmin 并删除外键本身。然后放下该列。

最佳答案

刚刚为我自己的项目解决了这个问题。删除外键时,需要连接表名和约束中的列,然后在名称后缀“_foreign”

http://laravel.com/docs/5.1/migrations#foreign-key-constraints

public function down()
{
        Schema::table( "post_field_properties", function( $table )
        {
            $table->dropForeign('post_field_properties_parent_id_foreign');
            $table->dropColumn('parent_id');
        });
}

关于php - 删除外键的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26628176/

相关文章:

php - 当特定规则失败时在重定向后执行操作 - Laravel 验证

php - 如何从用户输入的 Markdown 中删除脚本标签?

php - Laravel 验证规则 - 获取损坏的规则名称

javascript - 隐藏在手机上的元素,但不是桌面响应式布局

PHP 表单无法编辑 MySQL 表数据

mysql group by 中的 max 函数

mysql - 如何在 MySQL 中获取日期为今天、昨天或前一天的行?

MYSQL sql在表中查找计数大于x的id

javascript - 从 MySQL 导出到 PHP 中的对象数组,然后导出到 JavaScript

php - Symfony:类型为 "?Doctrine\Common\Collections\Collection"的预期参数,在属性路径中给出 "array"