laravel 如何在laravel迁移中更改枚举类型列?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33496518/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:08:18  来源:igfitidea点击:

How to change enum type column in laravel migration?

laravelenumsmigrationlaravel-5.1

提问by Vinod VT

I am using Laravel 5.1and I have a table called packages with this structure:

我正在使用Laravel 5.1,我有一个名为包的表,具有以下结构:

id              int(11)
weight          decimal(10,2)           
weight_unit     enum('Kg.', 'Gm.')

I would like to change the weight_unitenum to:

我想将weight_unit枚举更改为:

weight_unit enum('Grams','Kgs.','Pounds')

weight_unit enum('Grams','Kgs.','Pounds')

For this I create the following migration:

为此,我创建了以下迁移:

public function up()
{
    Schema::table('packages', function ($table) {
        $table->enum('weight_unit', array('Grams','Kgs.','Pounds'))->nullable()->change();
    });
}

But when I run the migration I receive an error:

但是当我运行迁移时,我收到一个错误:

Unknown database type enum requested, Doctrine\DBAL\Platforms\MySqlPlatform  

may not support it.

How can I change this enum?

我怎样才能改变这个枚举?

回答by Tayyab Hussain

Use the DB::statementmethod:

使用DB::statement方法:

DB::statement("ALTER TABLE packages MODIFY COLUMN weight_unit ENUM('Grams', 'Kgs', 'Pounds')");

回答by Ray Hunter

This worked for me when adding a new enum value to the modified enum column.

在向修改后的枚举列添加新的枚举值时,这对我有用。

Add the following to the up()method:

将以下内容添加到up()方法中:

DB::statement("ALTER TABLE packages MODIFY weight_unit ENUM('Grams', 'Kgs', 'Pounds', 'new value') NOT NULL");

Then in the down()method you can revert the change that was made:

然后在该down()方法中,您可以恢复所做的更改:

DB::statement("ALTER TABLE packages MODIFY weight_unit ENUM('Grams', 'Kgs', 'Pounds') NOT NULL");

Note: before the enum value is removed it needs to be changed to another enum value that will be retained.

注意:在删除枚举值之前,需要将其更改为另一个将保留的枚举值。

回答by Peter Kota

You can add custom constructor to migration and explain to Doctrine that enumshould be treated like string.

您可以将自定义构造函数添加到迁移并向 Doctrine 解释enum应该像字符串一样对待。

public function __construct(\Doctrine\DBAL\Migrations\Version $version)
{
    parent::__construct($version);

    $this->platform->registerDoctrineTypeMapping('enum', 'string');
}

回答by Pablo Ramires

$table->enum('level', ['easy', 'hard']);

回答by fico7489

add this before change() call :

在 change() 调用之前添加:

DB::getDoctrineSchemaManager()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');

回答by Alberto

In case you dont want to lose your dataand update it with the new values I came up with this solution:

如果您不想丢失数据并使用新值更新它,我想出了这个解决方案:

// Include old and new enum values
DB::statement("ALTER TABLE packages MODIFY COLUMN weight_unit ENUM('Kg.', 'Gm.', 'Grams', 'Kgs', 'Pounds')");
// Replace Kg. with Kgs
Packages::where('weight_unit', 'Kg.')->update(['weight_unit' => 'Kgs']);
// Replace Gm. with Grams
Packages::where('weight_unit', 'Gm.')->update(['weight_unit' => 'Grams']);
// Delete old values
DB::statement("ALTER TABLE packages MODIFY COLUMN weight_unit ENUM('Grams', 'Kgs', 'Pounds')");

This way you can replace your old values with the new ones.

这样您就可以用新值替换旧值。

回答by shivanikoko

Those who are still looking for the answer. This issue has been fixed in lastest Laravel Release 7.5 Have look (https://laravel-news.com/laravel-7-5-released).

那些仍在寻找答案的人。此问题已在最新的 Laravel 7.5 版中修复,请查看(https://laravel-news.com/laravel-7-5-released)。