kapitan
kapitan

Reputation: 2212

Migration - Cannot Change Double Data Type Value

I have an existing table created with this migration code:

Schema::create('mytable', function (Blueprint $table) {

 $table->increments('id');
 $table->double('mycolumn',8,2)->unsigned()->default(0);
 $table->timestamps();

});

Then I have created another migration file to adjust the value range of my mycolumn field with the migration file below.

Schema::table('mytable', function (Blueprint $table) {

 $table->double('mycolumn',15,2)->unsigned()->default(0)->change();

});

However I am getting an error:

In DBALException.php line 283:

  Unknown column type "double" requested. Any Doctrine type that you use has to be registered with \Doctrine\DBAL\Types\Type::addType(). You can get a li
  st of all the known types with \Doctrine\DBAL\Types\Type::getTypesMap(). If this error occurs during database introspection then you might have forgott
  en to register all database types for a Doctrine Type. Use AbstractPlatform#registerDoctrineTypeMapping() or have your custom types implement Type#getM
  appedDatabaseTypes(). If the type name is empty you might have a problem with the cache or forgot some mapping information.

What am I missing?

Upvotes: 12

Views: 17929

Answers (3)

Sethu
Sethu

Reputation: 1379

the double cannot be changed the way you do for other types, you can fix it using Doctrine\DBAL\Type

You can fix it in this way:

use Doctrine\DBAL\Types\FloatType;
use Doctrine\DBAL\Types\Type;

public function up() {
    if (!Type::hasType('double')) {
        Type::addType('double', FloatType::class);
    }
    Schema::table('mytable', function($table) {         
        $table->double('mycolumn',15,2)->default(0)->change();
        .....
    });
}

Upvotes: 27

Pedro Marthon
Pedro Marthon

Reputation: 101

I'm not sure if this will help, but you may use decimal or float instead of double in this case, though float will not work if you want a limited amount of decimal places. So it will look like:

$table->decimal('mycolumn',15,2)->unsigned()->default(0)->change();

Upvotes: 4

nakov
nakov

Reputation: 14268

You are missing this from the documentation

Only the following column types can be "changed": bigInteger, binary, boolean, date, dateTime, dateTimeTz, decimal, integer, json, longText, mediumText, smallInteger, string, text, time, unsignedBigInteger, unsignedInteger and unsignedSmallInteger.

So double cannot be changed.

I haven't tried but you can maybe use a RAW MySQL query like this, try it locally first of course:

DB::statement('alter table mytable modify mycolumn DOUBLE(15,2) DEFAULT 0');

Upvotes: 21

Related Questions