当前位置:主页 > 软件编程 > PHP代码 >

laravel解决迁移文件一次删除创建字段报错的问题

时间:2021-05-21 08:29:27 | 栏目:PHP代码 | 点击:

需求:通过写迁移文件更新user表中 topic 字段类型,从原来的varchar到json。

因为无法直接修改成json数据类型,只能采用先删除在创建的方式。

迁移文件代码如下:

<?php
 
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
 
class CreateUserTable extends Migration{
  /**
   * 运行迁移
   *
   * @return void
   */
  public function up()
  {
    Schema::create('user', function (Blueprint $table) {
      if (Schema::hasColumn('topic')) {
        $table->dropColumn('topic');
      }
      $table->json('topic')->comment('主题');
    });
  }
 
  /**
   * 撤销迁移
   *
   * @return void
   */
  public function down()
  {
    //
  }
}

执行迁移文件报错,提示topic这个字段已经存在。

但是很显然上面已经删除了,但是 删除创建分开两次执行,一切正常。

猜想:可能是迁移文件执行类型与实务,一起提交才成功。后续有机会验证

您可能感兴趣的文章:

相关文章