Sometimes it can be desirable to remove a database column that hosts a foreign key relationship (eg: in reverse migration scripts). It can be a bit of a hassle to get that done.

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.


Rss Comments

2 comments

  1. Thanks Robin

    #1 Sher Singh
  2. Thanks !

    #2 Felix Deimling

Leave a comment






Wordpress.org clearPaper by CreativeBits.it Copyright © 2012-2019 Robin Brackez. All rights reserved. By visiting this site you agree to accept cookies that are purely used to check how many visitors I have. Theme by: creativebits. Custom adaptations by Robin Brackez.