Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to update column attributes in Laravel 5.1 using CHAR datatype

Here is the code in my migration:

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();
    });
}

When I run migration, I get the following error:

[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.

When I checked \Doctrine\DBAL\Types\Type class, I found the following code:

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';

This means Doctrine does not support datatype CHAR. Is there any way to make it possible to change the column attributes in Laravel 5.1 with CHAR datatype?

like image 735
Debiprasad Avatar asked Oct 30 '22 23:10

Debiprasad


1 Answers

You have to add few lines

    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

like image 160
Shmuel Livshits Avatar answered Nov 10 '22 06:11

Shmuel Livshits