Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

issue with dropping foreign key

Tags:

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.

like image 671
Jimmyt1988 Avatar asked Oct 29 '14 10:10

Jimmyt1988


People also ask

What happens when you drop foreign key?

When you drop a foreign key using the DROP FOREIGN KEY clause of the ALTER TABLE statement, Db2 drops the corresponding referential relationships. (You must have the ALTER privilege on the dependent table and either the ALTER or REFERENCES privilege on the parent table.)

Can you drop a foreign key?

You can use the Control Center or the DROP statement to drop foreign keys.

Why foreign key is not recommended?

Having active foreign keys on tables improves data quality but hurts performance of insert, update and delete operations. Before those tasks database needs to check if it doesn't violate data integrity. This is a reason why some architects and DBAs give up on foreign keys at all.

What problems do foreign keys introduce?

Foreign key problems. Many database users encounter foreign key errors, often due to referential integrity problems. A foreign key might point to data that no longer exists, or the foreign key's data type doesn't match the primary key data type, eroding referential integrity.


2 Answers

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');         }); } 
like image 131
atm Avatar answered Oct 06 '22 01:10

atm


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”

Important Note: to see “Relation View” make sure your tables “Storage Engine” is InnoDB if not review this Q&A

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. 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

like image 28
Yousef Altaf Avatar answered Oct 06 '22 01:10

Yousef Altaf