Foreign key drop issue

My foreign key refers to its own table . This was to create messages with a hierarchy .

Now when I try to drop a 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 can do this is to go to phpmyadmin and delete the foreign key itself. and then drop the column.

+3


source to share


4 answers


Just figured it out for my own project. When you drop a foreign key you need to concatenate the table name and columns in the constraint and 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');
        });
}

      

+9


source


Try putting "_foreign" at 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');
        });
}

      

+1


source


To check the foreign key name, first create a backup of your database in .sql

there you will see your foreign key name 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 laravel 5.4 , it starts with this format: tablename_columnname_foreign

so in your laravel (here i am trying to remove foreign key from employee table)

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

      

0


source


Here's how to do it:

1) Login to your database and find the name of the foreign key relationship. If you are using phpmyadmin, go to the spreadsheet, go to the Structure tab, click the Link Type link and wait a few seconds for it to load. Find the "Constraint name" field. In my example, this is: "contrib_copyright_id_foreign"

2) Go to the Laravel migration script (or create). The trick is to turn off the foreign key relationship first 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 drop a table that has a foreign key, you first need to drop the foreign key relationship.

copied from here

Hope this helps someone

0


source







All Articles