framework: [L5.1] Migrations - Cannot change an existing column to type char
Trying to write a migration to change a column from VARCHAR to CHAR, but when I run the migration it throws 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 err
or 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.
Here is what my up
function looks like
public function up()
{
// dd(\Doctrine\DBAL\Types\Type::getTypesMap());
Schema::table('perk_word_search_boards', function (Blueprint $table) {
$table->char('uuid', 36)->change();
});
}
Interestingly though if I add a new column of type char, the migration runs just fine.
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Comments: 29 (14 by maintainers)
Commits related to this issue
- Document unchangeable column types. As suggested by @GrahamCampbell in issue [#9636](https://github.com/laravel/framework/issues/9636) — committed to Kender2/docs by Kender2 8 years ago
use Doctrine\DBAL\Types\StringType; use Doctrine\DBAL\Types\Type;
public function up() { if (!Type::hasType(‘char’)) { Type::addType(‘char’, StringType::class); }
}
Closing this as it’s not an issue with laravel/framework.
@subratpalhar92 Pretty sure that results in a
VARCHAR
column being created, notCHAR
.@phroggyy The function
change()
is included: http://laravel.com/docs/5.1/migrations#modifying-columnsHowever the documentation also says you need to have the package
doctrine/dbal
installed.@phroggyy Fair enough! Then the documentation needs some updating 😉
I was having this exact problem trying to change $table->string(‘…’) to $table->longText(‘…’), it didn’t work in Laravel 5.0.
But trying out:
$table->string('column', 4294967295)->change();
updated the field to LONGTEXT(4294967295).
This is possibly related, so I’ll post it here (please let me know if not, and I’ll create a new issue):
When trying to change a CHAR(36) column to a VARCHAR(50) column it changes the length but not the type. This column is also indexed, so it may be related to the above comment.
I noticed that running a SQL statement also did not work, so it is probably a database limitation.
However, using the change() method still does not work with the indexes removed. Running the sql statement with the indexes removed did indeed change the field type to VARCHAR.
Changing
->char('name', 1)
to->string('name', 1)
worked for me.Thank @andfelzapata . I am editing an existing file. I managed to change the column type with out rollback. You can read it here: http://techcater.com/update-column-types-laravel-5-2/