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?
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.
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.
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.
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.
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
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']); }); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With