如何在SQLite中使用Laravel迁移删除列?

3
我想为我的应用程序编写测试,但在运行迁移时出现了错误。 There is no column with name 我的迁移文件:
Schema::table('bill_payments', function (Blueprint $table) {
    $table->dropColumn('attachment');
    // $table->getColumns(); return empty array
    // $table->dropColumn(['attachment']); I tried this
});

dd(Schema::hasColumn('bill_payments', 'attachment')); // Return false

// and this is not working because return false.

if(Schema::hasColumn('bill_payments', 'attachment'))
{
    Schema::table('bill_payments', function (Blueprint $table) {
        $table->dropColumn('attachment');
    });
}

我添加了doctrine/dbal 2.5.13

我正在使用mysql运行测试,但速度很慢。

[已解决]

哇!我为表使用了前缀。我删除了它,现在它可以正常工作。


你有这个列吗?你在哪里创建它? - Felippe Duarte
你能确认该列是否真的存在吗?没有任何问题。 - abr
是的,在运行之前创建了它,并且当我检查数据库时看到了这一列。 - Berkay
好的。最简单的方法是删除你的测试数据库中的所有内容 .sqlite 文件并重新运行。 - Felippe Duarte
我尝试了,还删除了sqlite文件并重新创建,但仍然不起作用。 - Berkay
使用更新版本的 Laravel(9.44+),您不再需要使用 Doctrine/DBAL。 - Felix Geenen
1个回答

4

关于迁移,你需要知道的一件事是它们会一直运行直到崩溃或成功,根据你的示例:

Schema::table('bill_payments', function (Blueprint $table) {
    $table->dropColumn('attachment');
});
//It will drop the column and stop here. When you run the migration again, it will output your error because the column no longer exists.
dd(Schema::hasColumn('bill_payments', 'attachment')); // Return false

你的迁移代码应该在Down()方法中拥有相反的操作。这意味着你运行迁移时,它会应用Up()方法,当你回滚时,它会正确地还原。那个错误实际上就是它的意思,它表示当它到达涉及表bill_payments和列attachment的操作时,它会认为attachment不存在。
编辑:
文档中与SQlite相关的内容如下: "在使用SQLite数据库时,在单个迁移中删除或修改多个列不受支持。"

我实际上添加了dd和其他行进行调试。最初我有向上和向下的方法,并在“向上”方法中删除列,在“向下”方法中创建列。 - Berkay
我读了这个,但我只尝试删除一个列而不是多个。我错过了什么? - Berkay
使用SQLite 3.35.0+,列删除和重命名已在sqlite中实现。 - Felix Geenen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接