Search code examples
phpmysqllaravellaravel-artisan

issue with dropping foreign key


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.


Solution

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