Framework: 将列类型更改为 json 时出错

创建于 2015-11-27  ·  3评论  ·  资料来源: laravel/framework

不确定这是否是一个错误,但是当将列类型从 varchar 更改为 json 时会导致以下错误:

Schema::table('fields', function (Blueprint $table) {
    $table->json('label')->change();
});
  [Doctrine\DBAL\DBALException]
  Unknown column type "json" requested. Any Doctrine type that you use has to be registered
  with \Doctrine\DBAL\Types\Type::addType(). You can get a list of all the known types with
  \Doctrine\DBAL\Types\Type::getTypesMap(). If this error occurs during database introspecti
  on then you might have forgot to register all database types for a Doctrine Type. Use Abst
  ractPlatform#registerDoctrineTypeMapping() or have your custom types implement Type#getMap
  pedDatabaseTypes(). If the type name is empty you might have a problem with the cache or f
  orgot some mapping information.

改用$table->text('label')->change();吗?

最有用的评论

即使我使用的是使用 Laravel 5.3 的 PostgreSQL 数据库,我也会收到此错误

所有3条评论

问题在于 Doctrine DBAL,它还不支持 MySQL 的 JSON 列类型:

https://github.com/doctrine/dbal/blob/2.0.x/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php#L598

我目前看不到任何 PR,但您可以添加自定义地图类型(如错误消息中的建议):

http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/cookbook/custom-mapping-types.html

即使我使用的是使用 Laravel 5.3 的 PostgreSQL 数据库,我也会收到此错误

$query = "ALTER TABLE fields MODIFY label JSON DEFAULT NULL";
\Illuminate\Support\Facades\DB::statement($query);

在 laravel 迁移中试试这个.. 为我工作

此页面是否有帮助?
0 / 5 - 0 等级

相关问题

felixsanz picture felixsanz  ·  3评论

ghost picture ghost  ·  3评论

Anahkiasen picture Anahkiasen  ·  3评论

iivanov2 picture iivanov2  ·  3评论

kerbylav picture kerbylav  ·  3评论