laravel 如何创建迁移以在 eloquent 中为枚举添加值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21872121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how can I create a migration to add a value to an enum in eloquent
提问by user391986
I have a table that contains an enum field
我有一个包含枚举字段的表
CREATE TABLE `user_status` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`values` enum('on', 'off'),
PRIMARY KEY (`id`),
) ENGINE=InnoDB;
how can I create a migration to add a value to the enum field?
如何创建迁移以向枚举字段添加值?
回答by Alexandre Butynski
Laravel doesn't provide methods to update an enum column. You can delete and recreate the column but you could loose data during the operation and it's not really clean.
Laravel 不提供更新枚举列的方法。您可以删除并重新创建该列,但您可能会在操作过程中丢失数据,而且它不是很干净。
In this case, I think that the best choice is to write raw SQL into a migration :
在这种情况下,我认为最好的选择是将原始 SQL 写入迁移:
public function up()
{
DB::statement("ALTER TABLE user_status MODIFY COLUMN values ENUM('on','off','unknown')");
}
public function down()
{
DB::statement("ALTER TABLE user_status MODIFY COLUMN values ENUM('on','off')");
}
I could have made a mistake in the SQL syntax, I have never used ENUM
, but you can see the idea anyway.
我可能在 SQL 语法中犯了错误,我从未使用过ENUM
,但无论如何您都可以看到这个想法。
回答by larsemil
I did it with MySql:
我用 MySql 做到了:
class ChangeJobTypeEnum extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
DB::statement("ALTER TABLE _TABLENAME_ CHANGE _COLUMNNAME_ _COLUMNNAME_ ENUM('on', 'off', 'auto')");
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
DB::statement("ALTER TABLE _TABLENAME_ CHANGE _COLUMNNAME_ _COLUMNNAME_ ENUM('on', 'off')");
}
}
回答by dos4dev
I had a slightly different situation, it was necessary to add new items, change existing and remove old. This is my example.
我的情况略有不同,有必要添加新项目,更改现有项目并删除旧项目。这是我的例子。
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class ChangeEnum extends Migration
{
public function up()
{
Schema::table('table_example', function (Blueprint $table) {
DB::statement("ALTER TABLE table_example MODIFY status enum('first', 'second', 'third', 'fourth', 'fifth', 'sixth') NOT NULL;");
DB::statement("UPDATE `field` set `status` = 'fourth' where `status` = 'first';");
DB::statement("UPDATE `field` set `status` = 'fifth' where `status` = 'second';");
DB::statement("ALTER TABLE table_example MODIFY status enum('third', 'fourth', 'fifth', 'sixth') NOT NULL;");
});
}
public function down()
{
Schema::table('table_example', function (Blueprint $table) {
DB::statement("ALTER TABLE table_example MODIFY status enum('first', 'second', 'third', 'fourth', 'fifth', 'sixth') NOT NULL;");
DB::statement("UPDATE `field` set `status` = 'first' where `status` = 'fourth';");
DB::statement("UPDATE `field` set `status` = 'second' where `status` = 'fifth';");
DB::statement("ALTER TABLE table_example MODIFY status enum('first', 'second', 'third',) NOT NULL;");
});
}
}
By the way, generate row SQL query via JetBrains ide(DataGrip), is like that:
顺便说一句,通过JetBrains ide(DataGrip)生成行SQL查询,是这样的:
∧_∧
(??ω??)つ━☆?*。
? ノ ?゜+.
しーJ °。+ *′¨)
回答by Bhola Khawas
The second answer works, but in my case CHANGE
was throwing an error. So i tried using MODIFY
instead and that worked. Thanks guys..
第二个答案有效,但在我的情况下CHANGE
是抛出错误。所以我尝试MODIFY
改用它并且奏效了。谢谢你们..
Here is my code:
这是我的代码:
class ChangeJobTypeEnum extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
DB::statement("ALTER TABLE _TABLENAME_ MODIFY _COLUMNNAME_ ENUM('on', 'off', 'auto')");
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
DB::statement("ALTER TABLE _TABLENAME_ MODIFY_COLUMNNAME_ ENUM('on', 'off')");
}
}