laravel - 无法使用 CHAR 数据类型更新 Laravel 5.1 中的列属性

标签 laravel doctrine-orm doctrine laravel-5.1

这是我迁移中的代码:

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 中的列属性?

最佳答案

你必须添加几行

    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

关于laravel - 无法使用 CHAR 数据类型更新 Laravel 5.1 中的列属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31451112/

相关文章:

javascript - 在javascript数组中动态添加数据

php - 如何从 bash 文件运行 Laravel 项目?

php - 使用 session 在登录表单上显示错误的问题

Laravel livewire 属性嵌套绑定(bind)不显示表单字段中的值

mysql - 两张表,两种关系

php - 如何在 Symfony 中使用 1 twig 中的 2 种形式?

php - 如何通过引用文档查询嵌入文档?

php - Zend Framework 2 + Doctrine 2 框架应用程序

PostgreSQL 咨询锁不适用于 Doctrine 的 DBAL

php - Symfony3 只将占位符值保存到数据库