laravel 删除外键的问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26628176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:20:36  来源:igfitidea点击:

issue with dropping foreign key

phpmysqllaravelartisan

提问by Jimmyt1988

My foreign key relates to its own table. This was to produce posts with hierarchy.

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

Now when I try and drop the column in the database, it gives me this error:

现在,当我尝试删除数据库中的列时,它给了我这个错误:

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

This is the code:

这是代码:

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

The only way I seem to be able to do it, is to goto phpmyadmin and remove the foreign key itself. and then drop the column.

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

回答by atm

Just figured this out for my own project. When you are dropping a foreign key, you need to concatenate the table name and the columns in the constraint then suffix the name with "_foreign"

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

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

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

回答by Yousef Altaf

Here's how to do it:

这是如何做到的:

1) Log in to your database and lookup the name of the foreign key relationship. If you use phpmyadmin, go to the table, click the “Structure” tab, click the link “Relation View” and wait a few seconds for it to load. Search for the field “Constraint name”. In my example this is: “contribution_copyright_id_foreign”

1) 登录到你的数据库并查找外键关系的名称。如果您使用 phpmyadmin,请转到该表,单击“结构”选项卡,单击“关系视图”链接并等待几秒钟以使其加载。搜索字段“约束名称”。在我的例子中,这是:“contribution_copyright_id_foreign”

2) Go to the Laravel migration script (or create one). The trick is to first drop the foreign key relationship and then drop the column.

2) 转到 Laravel 迁移脚本(或创建一个)。诀窍是首先删除外键关系,然后删除列。

public function down()

 {

        Schema::table('contribution', function(Blueprint $table){

            $table->dropForeign('contribution_copyright_id_foreign');

            $table->dropColumn('copyright_id');

        });

If you want to remove a table where a foreign key is present, you also first have to drop the foreign key relationship.

如果要删除存在外键的表,还必须首先删除外键关系。

copied from here

这里复制

Hope it help someone

希望它可以帮助某人

回答by Nick Law

Try placing "_foreign" on the end of the column name. For example:

尝试将“_foreign”放在列名的末尾。例如:

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

回答by Anthony Kal

To check the name of the foreign key , first backup your database to .sql

要检查外键的名称,首先将您的数据库备份到 .sql

there you will see the name of your foreign key like this :

在那里你会看到你的外键的名称,如下所示:

...
KEY `employees_parent_id_foreign` (`parent_id`),
CONSTRAINT `employees_parent_id_foreign` FOREIGN KEY (`parent_id`) REFERENCES `laravel_article` (`id`) ON DELETE CASCADE
...

in my case is laravel 5.4, it start by this format : tablename_columnname_foreign

在我的例子中是laravel 5.4,它以这种格式开始:tablename_columnname_foreign

so in your laravel (here i try to drop foreign key from employee table)

所以在你的 Laravel 中(在这里我尝试从员工表中删除外键)

Schema::table("employees", function( $table )
{
    $table->dropForeign('employees_parent_id_foreign');
    $table->dropColumn('parent_id');
});