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

2020-09-01 14:52:33

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

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

迁移文件代码如下:

<?php use IlluminateDatabaseSchemaBlueprint;use IlluminateDatabaseMigrationsMigration; 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这个字段已经存在。

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

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

相关文章 大家在看