atomCode
atomCode

Reputation: 902

CakePHP Migrations - How to specify scale and precision

I'm Running CakePhp 2.7 with Migrations Plugin and a Postgresql DB. Creating a field of type 'number' and specifying length 15,4 (scale 15, precision 4 - or any length) does not actually create the field with that precision and/or scale.

          ...
 'license_fee' => array(
   'type' => 'number',
   'null' => true,
   'length' => '15,6',
   'default' => 0
  ),
        ...

The field is created with the correct type (numeric) but with no scale/precision here is the Postgres description of the created field.

license_fee               | numeric | default 0

What I was expecting to see is this

license_fee               | numeric(15,6) | default 0

I also tried using 'type' => 'decimal' but same happened. This might not be supported by the migrations plugin but I just want to know if anyone knows for sure what's going on.

Upvotes: 1

Views: 6878

Answers (3)

William
William

Reputation: 305

For 3.10 version:

$table->addColumn('name', ['type' => 'decimal', 'length' => 10, 'precision' => 3])

At /vendor/cakephp/cakephp/src/Database/Schema/TableSchema.php are the valid keys that can be used in a column:

$_columnKeys = [
    'type' => null,
    'baseType' => null,
    'length' => null,
    'precision' => null,
    'null' => null,
    'default' => null,
    'comment' => null,
];

Upvotes: 0

dype
dype

Reputation: 520

Found here: http://docs.phinx.org/en/latest/migrations.html

In order to create a : decimal(9,3)

$table->addColumn('distance', 'decimal', [
            'default' => null,
            'null' => false,
            'precision'=>9,
            'scale'=>3
        ]);

Upvotes: 9

atomCode
atomCode

Reputation: 902

After further investigation and some help from Cake Development Corp. It turns out that the correct to way specify precision and scale is by using "limit" not "length" like I was attempting. So it should be like this:

'license_fee' => array(
   'type' => 'number',
   'null' => true,
   'limit' => '15,6', //this is where I was wrong by using length
   'default' => 0
),

This will work also if using 'type' => 'decimal' which is actually the same datatype. The end result is as expected:

license_fee               | numeric(15,6) | default 0

I hope this useful for someone.

Upvotes: 2

Related Questions