phinx icon indicating copy to clipboard operation
phinx copied to clipboard

Setting scale and precision for MySQL float column

Open cxj opened this issue 4 years ago • 3 comments

I believe I've found an undocumented way to correctly set the float(scale,precision) column type for MySQL using Phinx, but before I make a PR to add it to the documentation, I figured I'd better ask here if this is intended correct behavior, or lucky happenstance.

Example

I have a column with definition float(3,2) and I need to expand it to be float(4,2).

The following appears to work, but is undocumented in both the Phinx and CakePHP documentation.

public function up(): void
{
    $this->table('my_table')
        ->changeColumn('my_column', 'float', ['scale' => 2, 'precision' => 4])
        ->save();
}

cxj avatar Apr 28 '21 19:04 cxj

Refs open https://github.com/cakephp/migrations/issues/314 and https://github.com/cakephp/docs/pull/5871

dereuromark avatar Apr 28 '21 20:04 dereuromark

Also note that as of MySQL 8.0.17, the nonstandard FLOAT(M,D) and DOUBLE(M,D) syntax is deprecated and you should expect support for it to be removed in a future version of MySQL.

Depending on usage, you should use DECIMAL(M,D) to store exact numeric data values when it is important to preserve exact precision, for example with monetary data.

If you need store approximate numeric data values, use FLOAT without precision, and format number to necessary precision in application code instead.

garas avatar Apr 29 '21 09:04 garas

Postgres is actually already failing on this:

LINE 1: ... "sort" INTEGER NOT NULL  DEFAULT '0', "lat" REAL (10) NULL,...

So, yeah, scale and precision should be omitted for float, or decimal should be used.

We should probably clarify in docs.

dereuromark avatar Nov 13 '23 08:11 dereuromark