【问题标题】:How to drop 'morphs' columns in Laravel migration如何在 Laravel 迁移中删除“变形”列
【发布时间】:2019-06-12 21:26:06
【问题描述】:

我正在编辑一个表格,以便它使用多态关系:

public function up()
    {
        Schema::table('locations', function (Blueprint $table) {
            $table->morphs('location');
        });
    }

但我不知道扭转这种迁移的最佳方式。我是否必须删除它创建的两列和索引本身,或者有没有办法在 Laravel 的一行中做到这一点?谢谢。

【问题讨论】:

    标签: laravel eloquent laravel-migrations


    【解决方案1】:

    Blueprint api 中找到这个:

    public function dropMorphs($name, $indexName = null)
    {
        $this->dropIndex($indexName ?: $this->createIndexName('index', ["{$name}_type", "{$name}_id"]));
    
        $this->dropColumn("{$name}_type", "{$name}_id");
    }
    

    所以就$table->dropMorphs('location');

    【讨论】:

      【解决方案2】:

      如果有人在处理同一个问题时遇到这篇文章,仅使用像我这样的 SQLite DB,SQLite 不支持一次性删除列和索引。您需要手动完成,并将其拆分为多个修改,如下所示:

      public function down(){
          Schema::table('locations', function (Blueprint $table) {
              $table->dropcolumn('location_id');
          });
      
          Schema::table('locations', function (Blueprint $table) {
              $table->dropcolumn('location_type');
          });
      }
      

      【讨论】:

        猜你喜欢
        • 2018-01-30
        • 2014-02-03
        • 2019-01-22
        • 2017-09-03
        • 2017-07-30
        • 2018-11-10
        • 2018-09-23
        • 2017-11-17
        • 2017-10-19
        相关资源
        最近更新 更多