Laravel 数据库迁移 - renameColumn 错误 - 请求的未知数据库类型枚举
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29165259/
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
Laravel db migration - renameColumn error - Unknown database type enum requested
提问by Latheesan
I am using Laravel 4.2
. I have the following library loaded in my composer.json
我正在使用 Laravel 4.2
。我的库中加载了以下库composer.json
"doctrine/dbal": "2.4.*",
I created the following migration:
我创建了以下迁移:
class RenameDeliveryNotesColumnOnOrderHeaderTable extends Migration {
public function up()
{
Schema::table('order_header', function(Blueprint $table)
{
$table->renameColumn('delivery_notes', 'packing_notes');
});
}
}
Where delivery_notes
column type is text
.
其中delivery_notes
列类型是text
.
When I run the migration, I get the following error:
运行迁移时,出现以下错误:
[Doctrine\DBAL\DBALException] Unknown database type enum requested, Doctrine\DBAL\Platforms\MySqlPlatform may not support it.
[Doctrine\DBAL\DBALException] 请求未知的数据库类型枚举,Doctrine\DBAL\Platforms\MySqlPlatform 可能不支持它。
Any idea why I am getting this error? How should I go about fixing this? I need to rename a column in my table. Are there any alternative way to rename the column?
知道为什么我会收到此错误吗?我应该如何解决这个问题?我需要重命名表中的一列。有没有其他方法可以重命名列?
采纳答案by Ivanka Todorova
Laravel's documentationsays that:
Note: Renaming
enum
column types is not supported.
注意:
enum
不支持重命名列类型。
Here: https://github.com/laravel/framework/issues/1186
这里:https: //github.com/laravel/framework/issues/1186
You can find some workarounds about this issue. And since you said that this column is not enum
, take a look at @upngo's comment:
您可以找到有关此问题的一些解决方法。既然你说这个专栏不是enum
,那么看看@upngo的评论:
"...The issue is renaming ANYcolumn on a table that has an
enum
."
“...问题是重命名具有.的表上的任何列
enum
。”
Also I found this article that focuses on this issue and suggest an option that might help you.
我还发现这篇文章关注了这个问题,并提出了一个可能对你有帮助的选项。
回答by Gmatkowski
DB::getDoctrineSchemaManager()
->getDatabasePlatform()
->registerDoctrineTypeMapping('enum', 'string');
This works for me on Laravel 5.1
这在 Laravel 5.1 上对我有用
回答by Ihor Havryliv
I met this problem in Laravel version 5.1.19 (LTS). This is actual for earlier versions too. I wanted to inform you as I resolved the issue base on previous comments.
我在 Laravel 5.1.19 (LTS) 版本中遇到了这个问题。这对于早期版本也是实际的。我想通知你,因为我根据之前的评论解决了这个问题。
First of all, I tried next code in my migration file:
首先,我在迁移文件中尝试了下一个代码:
$table->renameColumn('column_name');
But after command php artisan migrate
, I got next error:
但是在 command 之后php artisan migrate
,我得到了下一个错误:
[Symfony\Component\Debug\Exception\FatalErrorException] Class 'Doctrine\DBAL\Driver\PDOMySql\Driver' not found
[Symfony\Component\Debug\Exception\FatalErrorException] 找不到类 'Doctrine\DBAL\Driver\PDOMySql\Driver'
As you know DBAL was removed from the laravel core and we need add it to the composer.json.(For example:"require": {"doctrine/dbal": "2.5.1"}
).
I set DBAL as required and tried again to do migrate command but got next error:
如您所知 DBAL 已从 laravel 核心中删除,我们需要将其添加到 composer.json。(例如:)"require": {"doctrine/dbal": "2.5.1"}
。我根据需要设置了 DBAL 并再次尝试执行 migrate 命令,但出现下一个错误:
[Doctrine\DBAL\DBALException]
Unknown database type enum requested, Doctrine\DBAL\Platforms\MySqlPlatform may not support it.
[Doctrine\DBAL\DBALException]
请求未知的数据库类型枚举,Doctrine\DBAL\Platforms\MySqlPlatform 可能不支持它。
Then I tried next raw sql in my migration file:
For up()
:
然后我在迁移文件中尝试了下一个原始 sql:对于up()
:
DB::statement("ALTER TABLE `table_name` CHANGE `old_column_name` `new_column_name` ENUM('first value', 'second_value', ...) DEFAULT 'first_value' AFTER `some_field`");
For down()
:
对于down()
:
DB::statement("ALTER TABLE `table_name` CHANGE `new_column_name` `old_column_name` ENUM('first value', 'second_value', ...) DEFAULT 'first_value' AFTER `some_field`");
and it works.
它有效。
P.S. For renaming other fields in the table which contains an enum field we should use same schema with raw sql as was written in previous comments.
PS 要重命名包含枚举字段的表中的其他字段,我们应该使用与之前评论中编写的原始 sql 相同的模式。
回答by luchaninov
You can add custom constructor to migration and explain to Doctrine that enum should be treated like string.
您可以将自定义构造函数添加到迁移并向 Doctrine 解释枚举应该被视为字符串。
public function __construct(\Doctrine\DBAL\Migrations\Version $version)
{
parent::__construct($version);
$this->platform->registerDoctrineTypeMapping('enum', 'string');
}
回答by Deric Lima
I had the same problem with Laravel 5.1 and PostGres.
So basically I used the DB::statement
to create the ENUM and solve the problem:
我在 Laravel 5.1 和 PostGres 上遇到了同样的问题。所以基本上我用DB::statement
来创建 ENUM 并解决问题:
DB::statement("CREATE TYPE e_users AS ENUM('data1','data2')");
DB::statement("CREATE TYPE e_users AS ENUM('data1','data2')");
And then:
进而:
DB::statement("ALTER TABLE users ADD COLUMN column e_users");
DB::statement("ALTER TABLE users ADD COLUMN column e_users");
回答by zeros-and-ones
Here is the answer for Laravel 5.2.45+ (might work in 5.1 as well, have not tested or checked yet, please let me know so I can update this question.)
这是 Laravel 5.2.45+ 的答案(可能也适用于 5.1,尚未测试或检查,请告诉我,以便我更新此问题。)
Add this line in you up method:
在 up 方法中添加这一行:
Schema::getConnection()->getDoctrineSchemaManager()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');
Something like this:
像这样的东西:
public function up()
{
Schema::getConnection()->getDoctrineSchemaManager()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');
Schema::table('users', function (Blueprint $table) {
$table->text('bio')->change();
});
}
回答by DfKimera
Though the original author had issues with Laravel 4, this can safely be fixed in Laravel 5 by bumping the version of doctrine/dbal
in your composer.json
to ^2.6
, as it was fixed in this PRon release 2.6.0
虽然原作者有问题与Laravel 4,这可以安全地通过碰撞的版本固定Laravel 5doctrine/dbal
在你composer.json
来^2.6
,因为它是固定在这个PR的版本2.6.0
Make sure to check for compatibility-breaking changesin the release changelog