SQLite在删除列时期望字符串作为索引列

ruoxqz4g  于 2023-04-21  发布在  SQLite
关注(0)|答案(2)|浏览(135)
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class RemoveConstructionDateToOrdersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        if (Schema::hasColumn('orders', 'construction_date')) {
            Schema::table('orders', function (Blueprint $table) {
                $table->dropColumn('construction_date');
            });
        }
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('orders', function (Blueprint $table) {
            $table->date('construction_date');
        });
    }
}

当我在sqlite数据库上迁移这个laravel迁移时,我得到了以下错误。
InvalidArgumentException:需要字符串作为索引列

9q78igpj

9q78igpj1#

经过几个小时的摸索,我找到了问题所在。看起来像是doctrine/dbal包无法重命名/handle/drop?!SQLite列已经有索引
因此,我的解决方案是不对后来在迁移中重命名的列创建任何索引
当然,我们确实希望索引存在于MySQL中,所以这里是我的解决方案。

Schema::table('comments', function (Blueprint $table) {
        $table->string('commentable_type', 100)->nullable()->after('id');

        //only add index if the connection is mysql
        if(config('DB_CONNECTION') === 'mysql'){
            $table->index('commentable_type');
        }
    });

因此,当您稍后尝试重命名列并使用SQLite时,它可以工作。

e5njpo68

e5njpo682#

SQLite不支持在一次修改中多次调用dropColumn / renameColumn。
回顾您的迁移文件,并将dropColumn / renameColumn分离到它们自己的修改块中。

相关问题