我该如何创建一个迁移以雄辩地为枚举添加值 [英] how can I create a migration to add a value to an enum in eloquent

查看:89
本文介绍了我该如何创建一个迁移以雄辩地为枚举添加值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含枚举字段的表

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?

推荐答案

Laravel不提供更新枚举列的方法。您可以删除并重新创建该列,但是在操作过程中可能会丢失数据,并且它实际上不是 clean

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.

在这种情况下,我认为最好的选择是将原始SQL写入迁移中:

In this case, I think that the best choice is to write raw SQL into a migration :

public function up()
{
    DB::statement("ALTER TABLE user_status MODIFY COLUMN ENUM('on','off','unknown')");
}

public function down()
{
    DB::statement("ALTER TABLE user_status MODIFY COLUMN ENUM('on','off')");
}

我可能在SQL语法中犯了一个错误,但我从未使用过 ENUM ,但您还是可以看到这个主意。

I could have made a mistake in the SQL syntax, I have never used ENUM, but you can see the idea anyway.

这篇关于我该如何创建一个迁移以雄辩地为枚举添加值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆