Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing column from database in Laravel 5+

Tags:

laravel

I've a blog for which the articles table Schema is defined like this:

public function up() {     Schema::create('articles', function (Blueprint $table) {         $table->increments('id');         $table->integer('user_id')->unsigned();         $table->string('title');         $table->string('thumb')->nullable();         $table->text('excerpt');         $table->text('body');         $table->string('slug')->unique();         $table->integer('comment_count')->unsigned()->default(0);         $table->integer('view_count')->unsigned()->default(0);         $table->timestamps();         $table->softDeletes(); }  public function down() {     Schema::drop('articles'); } 

I want to drop the columns comment_count and view_count without losing existing data in the table

I defined a new migration like this:

class RemoveCommentViewCount extends Migration {     public function up()     {         //nothing here     }      public function down()     {         Schema::table('articles', function($table) {            $table->dropColumn('comment_count');            $table->dropColumn('view_count');        });    } } 

and I did php artisan migrate . It did migrate successfully, but the two columns are not dropped.

What am I doing wrong? How can I drop those columns without losing the existing data in the table?

like image 596
byteseeker Avatar asked Jun 04 '16 15:06

byteseeker


People also ask

How do I remove a column from a database in migration?

To remove the columns you have to run the make migration command but instruct it as removing instead of creating a new migration. Make use of the "--table=your-table-name" flag and specify the table that you want to remove the columns.

How do I drop a column in MySQL?

Syntax. The syntax to drop a column in a table in MySQL (using the ALTER TABLE statement) is: ALTER TABLE table_name DROP COLUMN column_name; table_name.

How do you delete a column in Rails?

The change method can be used to drop a column in Rails 4 applications, but should not be used in Rails 3. I updated my answer accordingly. You can also use remove_column :table_name, :column_name, :type, :options within the change method, since if you specify the type reverting the migration is possible.

How do I add a column in laravel migration without losing data?

database\migration\add_new_column_to_products_table.php Now you can run migrate command to add this new field. Simply you can run below command to auto add a new field. Hope it can help you. Laravel Migration example tutorial, in this tutorial you have learned how to add new column to a table without losing data.


2 Answers

Your migration must look like this:

 Class RemoveCommentViewCount extends Migration   {       public function up()       {           Schema::table('articles', function($table) {              $table->dropColumn('comment_count');              $table->dropColumn('view_count');           });       }        public function down()       {           Schema::table('articles', function($table) {              $table->integer('comment_count');              $table->integer('view_count');           });       }   } 

The dropColumn in the up method, because with new migration you want to delete this columns. If you make a rollback, you have another time the two columns

like image 112
Sangar82 Avatar answered Oct 05 '22 17:10

Sangar82


Even you can drop the multiple columns in a single line by passing the array column to dropColumn function.

class RemoveCommentViewCount extends Migration {     public function up()     {         Schema::table('articles', function($table) {             $table->dropColumn(['comment_count', 'view_count']);         });     }      public function down()     {         Schema::table('articles', function($table) {             $table->integer('comment_count');             $table->integer('view_count');         });     } } 

In case you have a foreign key constraint, then drop first the foreign key index association and then can pass the column to dropColumn function with others like following.

public function up() {     Schema::table('customer_orders', function($table) {         $table->dropForeign(['product_id']);         $table->dropForeign(['shipping_address_id']);         $table->dropColumn(['product_id', 'shipping_address_id', 'column1', 'column2']);     }); } 
like image 44
Channaveer Hakari Avatar answered Oct 05 '22 16:10

Channaveer Hakari