Laravel 5.4 迁移 ENUM 在 MySQL 中失败
Laravel 5.4 migration ENUM fails in MySQL
当我尝试应用迁移时,出现此错误:
[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');
});
}
}
谢谢 ;)
可以找到 here 与 laravel 严格相关的信息。我强烈建议你阅读线程。这不是 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
在包含 ENUM 类型的迁移文件中添加具有以下内容的构造函数方法:
public function __construct() {
// Register ENUM type
DB::getDoctrineSchemaManager()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');
}
这在 Laravel 5.2 对我有用。您可以尝试在更高级别添加它,但这对我来说更快:)
当我尝试应用迁移时,出现此错误:
[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');
});
}
}
谢谢 ;)
可以找到 here 与 laravel 严格相关的信息。我强烈建议你阅读线程。这不是 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
在包含 ENUM 类型的迁移文件中添加具有以下内容的构造函数方法:
public function __construct() {
// Register ENUM type
DB::getDoctrineSchemaManager()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');
}
这在 Laravel 5.2 对我有用。您可以尝试在更高级别添加它,但这对我来说更快:)