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.
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.)
You can use the Control Center or the DROP statement to drop foreign keys.
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.
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.
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'); }); }
Here’s how to do it:
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”
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With