需求:通过写迁移文件更新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这个字段已经存在。
但是很显然上面已经删除了,但是 删除创建分开两次执行,一切正常。
猜想:可能是迁移文件执行类型与实务,一起提交才成功。后续有机会验证
以上这篇laravel解决迁移文件一次删除创建字段报错的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持w3xue。