issue with dropping foreign key

drop foreign key laravel
laravel cannot drop index needed in a foreign key constraint
laravel delete foreign key constraint
remove foreign key constraint mysql
drop foreign key sql
cannot drop indexneeded in a foreign key constraint
laravel drop composite key
cannot drop index user needed in a foreign key constraint

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.


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"

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

Why does dropping foreign keys take long?, Dropping a constraint requires a Sch-M (Schema Modification) lock that will block others to query the table during the modification. You are  To fix this issue we need to drop the foreign key, apply the snapshot and then re-create the foreign key. We can manually run the script to drop foreign keys and recreate them after applying snapshot. But in case of many subscribers it would be bit difficult to run scripts manually.


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"

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.

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

SQL Server: Drop a foreign key, Learn how to drop a foreign key in SQL Server with syntax and examples. Once a foreign key has been created, you may find that you wish to drop the foreign  The syntax to drop a foreign key in SQL Server (Transact-SQL) is: ALTER TABLE table_name DROP CONSTRAINT fk_name; Parameters or Arguments table_name The name of the table where the foreign key has been created. fk_name The name of the foreign key that you wish to remove.


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

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

Dropping foreign key constraints · Issue #1871 · coleifer/peewee , I have a situation with circular foreign key dependencies, and I followed the approach of using a DeferredForeignKey. I am using MySQL. Just be aware that it is dangerous to drop a foreign key constraint without knowing why it is there inthe first place. If you just created this and did it by mistake then use the code provided in the other answers. If not, then do not drop the constraint until you are sure that you won't break something else by doing so.


To check the name of the foreign key , first backup your database to .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

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

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

Ability to drop foreign key · Issue #157 · knex/knex · GitHub, I am testing Knex in a NodeJS application and found a thing that makes sense for migrations: The hability to drop foreign keys in Schema,  To delete a foreign key constraint. In Object Explorer, expand the table with the constraint and then expand Keys. Right-click the constraint and then click Delete. In the Delete Object dialog box, click OK. Using Transact-SQL To delete a foreign key constraint. In Object Explorer, connect to an instance of Database Engine.


Delete Foreign Key Relationships, Deleting a foreign key constraint removes the requirement to enforce referential integrity. In This Topic. Before you begin: Security. To delete a  If the referenced key is a non-primary unique constraint, then we also need a column list from the referenced table; So far, I have not seen this being supported by any dialect. We can emulate it by either: Querying the dictionary views and dropping whatever constraint we find matching the criteria (default)


Dropping foreign keys, To drop foreign keys using the Control Center: Expand the object tree until you see the Tables folder. Right-click the table you want to modify, and select Alter  Drop and Re-Create All Foreign Key Constraints in Getting Started with HDInsight - Part 2 - Introduc Using DELETE CASCADE Option for Foreign Keys Drop and Re-Create All Foreign Key Constraints in Disable, enable, drop and recreate SQL Server Fore More Database Developer Tips


Dropping constraints, Statements that insert or update the table containing the foreign key; Statements that update or delete the parent table. Parent topic: Constraints. Task  SQL FOREIGN KEY Constraint. A FOREIGN KEY is a key used to link two tables together. A FOREIGN KEY is a field (or collection of fields) in one table that refers to the PRIMARY KEY in another table. The table containing the foreign key is called the child table, and the table containing the candidate key is called the referenced or parent table.