在Laravel 5.2中从数据库中删除列

时间:2016-06-04 15:02:57

标签: laravel laravel-5.2

我有一个博客,articlesSchema的定义如下:

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

我想删除列comment_countview_count,而不会丢失表格中的现有数据

我定义了一个像这样的新迁移:

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

我做了php artisan migrate。它确实成功迁移,但两列不会被删除。

我做错了什么?如何在不丢失表中现有数据的情况下删除这些列?

5 个答案:

答案 0 :(得分:62)

您的迁移必须如下所示:

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

up方法中的dropColumn,因为您要删除此列的新迁移。如果您进行回滚,则还有两列

答案 1 :(得分:2)

甚至可以通过将数组列传递给 dropColumn 函数来将多列放在一行中。

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

如果你有 foregin key 约束,那么首先删除外键索引关联,然后可以将列传递给dropColumn函数,其他人如下所示

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

答案 2 :(得分:0)

只需将此代码添加到migration.php文件中的 down()函数中

Schema::table('articles', function (Blueprint $table) {
   $table->integer('comment_count')->unsigned()->default(0);
   $table->integer('view_count')->unsigned()->default(0);
});

然后运行 ​​- > php artisan migrate:rollback

答案 3 :(得分:0)

我遇到了类似的问题,我编辑了初始迁移,即初始表架构,删除了列,然后运行 php artisan migration:refresh ,它对我有用

答案 4 :(得分:0)

创建删除列迁移

 php artisan make:migration RemoveCommentViewCount

down方法用于回滚,因此在up()函数中添加dropColumn并在down()中反转

<?php
  
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class RemoveCommentViewCount extends Migration
{
    public function up()
    {
        if (Schema::hasColumn('comment_count', 'view_count')){
  
            Schema::table('articles', function (Blueprint $table) {
                $table->dropColumn('comment_count');
                $table->dropColumn('view_count');

                //OR
                $table->dropColumn(['comment_count', 'view_count']);
            });
        }
    }

    public function down()
    {
        Schema::table('articles', function($table) {
           $table->integer('comment_count');
           $table->integer('view_count');
       });

   }
}

查看dropping columns migrations上的laravel文档