gpt4 book ai didi

php - Laravel 5.4 迁移 ENUM 在 MySQL 中失败

转载 作者:可可西里 更新时间:2023-11-01 07:55:51 28 4
gpt4 key购买 nike

当我尝试应用迁移时,出现此错误:

[Doctrine\DBAL\DBALException]
Unknown database type enum requested, Doctrine\DBAL\Platforms\MySQL57Platform may not support it.

应用了迁移,在数据库上创建了枚举列,我得到了错误,所以我不能执行下一个迁移,因为这个迁移抛出了这个错误。

在服务器中,我有 MySQL 版本 5.7.17

这是我的迁移代码:

class AddDocumentUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('document', 9)->unique();
$table->enum('document_type', ['dni', 'nie', 'nif', 'cif']);
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('document');
$table->dropColumn('document_type');
});
}
}

谢谢;)

最佳答案

可以找到与 laravel 严格相关的信息 here .我强烈建议你阅读线程。这不是 laravel 问题,它一直是 Doctrine 中的一个错误。

在上面的问题线程中,用户 henritoivar 有一个有趣的想法。

此处引用:

This worked for me in laravel 5.2 with doctrine/dbal@^2.5 . When you have an enum on your table and you want to change any of the columns on the table you will have to:

public function up()
{
Schema::getConnection()->getDoctrineSchemaManager()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');

Schema::table('jobs', function(Blueprint $table)
{
$table->decimal('latitude', 10, 6)->nullable()->change();
});
}

我不知道这是否适合您,但值得一试。


我本来会把它作为评论发布的,但读起来太他妈长了。

From the official Doctrine documents :

The type system of Doctrine 2 consists of flyweights, which means there is only one instance of any given type. Additionally types do not contain state. Both assumptions make it rather complicated to work with the Enum Type of MySQL that is used quite a lot by developers. When using Enums with a non-tweaked Doctrine 2 application you will get errors from the Schema-Tool commands due to the unknown database type “enum”. By default Doctrine does not map the MySQL enum type to a Doctrine type. This is because Enums contain state (their allowed values) and Doctrine types don’t.

从技术上讲,这是可以解决的。参见 here .但这与 Laravel 所基于的 symfony 严格相关。


Laravel's docs also stated that it has a problem with enums :

Renaming any column in a table that also has a column of type enum is >not currently supported.


虽然这不是答案,但我希望它能为您指明正确的方向,或者至少让您了解自己所面临的问题。


更多相关问题:

How to enable ENUMs in Symfony 2 / Doctrine

Laravel 5.1 Unknown database type enum requested

关于php - Laravel 5.4 迁移 ENUM 在 MySQL 中失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42072360/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com