您根本不应该使用枚举。 即使使用laravel 5.8,问题也无法解决。
感谢所有提醒我的人
Laravel 5.1官方文档指出:
注意:目前不支持使用enum列重命名表中的列。
我需要补充一点,将可用选项添加到enum列声明中时,您将遇到同样的问题。
这使我得出一个结论,您应谨慎使用枚举。 甚至根本不应该使用枚举。
我无法投票通过字符串替换枚举的任何答案。 否,您需要创建一个查找表,并将枚举替换为enum,将其作为foreign key。
这项工作很繁琐,没有单元测试的覆盖范围,您会很烦恼,但这是正确的解决方案。
您可能会因为正确执行此操作而被解雇,因为这花费了太长时间,但是,不用担心,您会找到更好的工作。 :)
这是将可用选项添加到enum列声明中会多么困难的示例
说你有这个:
Schema::create('blogs', function (Blueprint $table) {
$table->enum('type', [BlogType::KEY_PAYMENTS]);
$table->index(['type', 'created_at']);
...
您需要提供更多类型
public function up(): void
{
Schema::table('blogs', function (Blueprint $table) {
$table->dropIndex(['type', 'created_at']);
$table->enum('type_tmp', [
BlogType::KEY_PAYMENTS,
BlogType::KEY_CATS,
BlogType::KEY_DOGS,
])->after('type');
});
DB::statement('update `blogs` as te set te.`type_tmp` = te.`type` ');
Schema::table('blogs', function (Blueprint $table) {
$table->dropColumn('type');
});
Schema::table('blogs', function (Blueprint $table) {
$table->enum('type', [
BlogType::KEY_PAYMENTS,
BlogType::KEY_CATS,
BlogType::KEY_DOGS,
])->after('type_tmp');
});
DB::statement('update `blogs` as te set te.`type` = te.`type_tmp` ');
Schema::table('blogs', function (Blueprint $table) {
$table->dropColumn('type_tmp');
$table->index(['type', 'created_at']);
});
}