【问题标题】:Laravel migrations: dropping a specific tableLaravel 迁移:删除特定表
【发布时间】:2016-09-11 23:11:46
【问题描述】:

有什么方法/laravel-command 可以从生产服务器中删除特定的表吗?

【问题讨论】:

    标签: laravel database-migration


    【解决方案1】:

    设置迁移。

    运行此命令以设置迁移:

    php artisan make:migration drop_my_table
    

    然后您可以像这样构建您的迁移:

    <?php
    
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Database\Migrations\Migration;
    
    class DropMyTable extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            // drop the table
            Schema::dropIfExists('my_table');
        }
    
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            // create the table
            Schema::create('my_table', function (Blueprint $table) {
                $table->increments('id');
                // .. other columns
                $table->timestamps();
            });
        }
    }
    

    您当然可以直接删除而不检查是否存在:

    Schema::drop('my_table');
    

    在此处的文档中进一步阅读:

    https://laravel.com/docs/5.2/migrations#writing-migrations

    您可能还必须考虑删除任何现有的外键/索引,例如,如果您想删除主键:

    public function up()
    {
        Schema::table('my_table', function ($table) {
            $table->dropPrimary('my_table_id_primary');
        });
    
        Schema::dropIfExists('my_table');
    }
    

    更多关于删除索引等的文档:

    https://laravel.com/docs/5.2/migrations#dropping-indexes

    【讨论】:

      猜你喜欢
      • 2017-11-17
      • 2017-02-23
      • 2014-02-03
      • 2017-07-30
      • 2018-01-30
      • 2015-08-05
      • 2019-04-09
      • 2016-06-19
      • 2021-02-28
      相关资源
      最近更新 更多