【问题标题】:Unable to update column attributes in Laravel 5.1 using CHAR datatype无法使用 CHAR 数据类型更新 Laravel 5.1 中的列属性
【发布时间】:2015-07-16 10:01:27
【问题描述】:

这是我迁移中的代码:

public function up()
{
    Schema::table('companies', function (Blueprint $table) {
        $table->char('default_unit_for_weight', 2)->default('kg')->after('notes')->change();
        $table->char('default_currency', 3)->default('EUR')->after('default_unit_for_weight')->change();
    });
}

当我运行迁移时,我收到以下错误:

[Doctrine\DBAL\DBALException] Unknown column type "char" 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 introspection then you might have forgot to register all database types for a Doctrine Type. Use AbstractPlatform#registerDoctrineTypeMapping() or have your custom types implement Type#getMappedDatabaseTypes(). If the type name is empty you might have a problem with the cache or forgot some mapping information.

查看\Doctrine\DBAL\Types\Type类时,发现如下代码:

const TARRAY = 'array';
const SIMPLE_ARRAY = 'simple_array';
const JSON_ARRAY = 'json_array';
const BIGINT = 'bigint';
const BOOLEAN = 'boolean';
const DATETIME = 'datetime';
const DATETIMETZ = 'datetimetz';
const DATE = 'date';
const TIME = 'time';
const DECIMAL = 'decimal';
const INTEGER = 'integer';
const OBJECT = 'object';
const SMALLINT = 'smallint';
const STRING = 'string';
const TEXT = 'text';
const BINARY = 'binary';
const BLOB = 'blob';
const FLOAT = 'float';
const GUID = 'guid';

这意味着 Doctrine 不支持数据类型 CHAR。有什么方法可以用 CHAR 数据类型更改 Laravel 5.1 中的列属性?

【问题讨论】:

  • 我正在研究这个问题,据我所知这是已知问题,我们只需要处理它。一种可能的方法是将该列的内容复制到临时表/字段,删除该列并重新创建它。然后将内容复制回来。这是太多的工作,但它应该完成工作。

标签: laravel doctrine-orm doctrine laravel-5.1


【解决方案1】:

你必须添加几行

    use Doctrine\DBAL\Types\StringType;
    use Doctrine\DBAL\Types\Type;
    
    public function up() {
        if (!Type::hasType('char')) {
            Type::addType('char', StringType::class);
        }
Schema::table('table_name', function (Blueprint $table) {
            $table->char('default_unit_for_weight', 2)->default('kg')->after('notes')->change();
            $table->char('default_currency', 3)->default('EUR')->after('default_unit_for_weight')->change();
        }
}

https://github.com/laravel/framework/issues/27518

【讨论】:

    【解决方案2】:

    您可以使用原始查询进行迁移 (DB::statement('query')),但我不确定 Eloquent 是否不会恐慌,但它不应该

    【讨论】:

      猜你喜欢
      • 2016-03-07
      • 2018-09-20
      • 1970-01-01
      • 2021-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-15
      相关资源
      最近更新 更多