Friday 16 June 2017

Laravel : Database Migration ( Key Constraint )

Database Migration

What are steps to be looked from cli command "php artisan migrate:refresh --seed" . First of as we this command will drop table migration from bottom to top . However in our case, we should declare a foreign key migration table exactly at bottom of the folder. This is to ensure we can drop child key and the relation of foreign key with another table before dropping parent keys.


DropForeign ( ) need to be call under Schema::table and with a blue print object


Schema::table('courses', function (Blueprint $table) {
        $table->dropForeign('courses_post_id_foreign');    
});

or


Schema::table('courses', function (Blueprint $table) {        
       $table->dropForeign(['your_key_name']);
});

Next, after removing the relation and the key constraint of foreign key. Now we can drop the column inside the class table .


   Schema::table('courses', function (Blueprint $table) {
         $table->dropForeign('courses_blocks_idx_1');
         $table->dropColumn('faculty_id');
       });

References :

https://stackoverflow.com/questions/41416993/call-to-undefined-method-illuminate-database-schema-mysqlbuilderdropforeign

https://laravel.com/docs/5.4/migrations

No comments:

Post a Comment