How can I use
->addColumn('value', 'decimal', ['limit' => 8.2, 'null' => false])
in phinx?
I have this, but it gives an error.
Can you help me please
Hi,
What database are you using?
What is the exact text of the error?
Have you checked the docs at Writing Migrations — Phinx 0.5.3 documentation and the decimal options precision, scale & signed?
Thanks for replying.
I use MySQL, and “robmorgan/phinx”: “^0.16.2”,
This is what I have:
->addColumn('interes', 'decimal', ['limit' => 8.2, 'null' => false])
->addColumn('multas', 'decimal', ['limit' => 8.2, 'null' => false])
->addColumn('gasto_comun', 'decimal', ['limit' => 8.2, 'null' => false])
And this is the error:
TypeError: Phinx\Db\Table\Column::setLimit(): Argument #1 ($limit) must be of type ?int, float given, called in /home/servpcspa/web/servpcspa.com/public_html/vendor/robmorgan/phinx/src/Phinx/Db/Table/Column.php on line 802 and defined in /home/servpcspa/web/servpcspa.com/public_html/vendor/robmorgan/phinx/src/Phinx/Db/Table/Column.php:228
I have reviewed the information, but it is not clear, an example is missing, to know how to apply
https://dev.mysql.com/doc/refman/8.4/en/precision-math-decimal-characteristics.html
From what you put in limit
I’m guessing you might want to be able to enter 8
digits before the decimal point and 2
after the decimal point (e.g. 12345678.12) so you probably need a precision of 10
and a scale of 2
$this->table('test')
->addColumn('value', 'decimal', [
'precision' => 10, // total digits including decimals e.g. 12345678.12 = 10 digits
'scale' => 2, // digits after the decimal point
'signed' => true, // false for unsigned, for true signed
'null' => false
])->create();
This is what the table definition looks like in MySQL Workbench
CREATE TABLE `test` (
`id` int NOT NULL AUTO_INCREMENT,
`value` decimal(10,2) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
I did what was recommended, but it still gives an error on the following line:
->addColumn('interes', 'decimal', ['precision' => 10, 'scale' => 2, 'signed' => false, 'null' => false])
RuntimeException: "unsigned" is not a valid column option. in /home/servpcspa/web/servpcspa.com/public_html/vendor/robmorgan/phinx/src/Phinx/Db/Table/Column.php:798
Stack trace:
#0 /home/servpcspa/web/servpcspa.com/public_html/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/AbstractAdapter.php(292): Phinx\Db\Table\Column->setOptions()
#1 /home/servpcspa/web/servpcspa.com/public_html/vendor/robmorgan/phinx/src/Phinx/Db/Adapter/AdapterWrapper.php(138): Phinx\Db\Adapter\AbstractAdapter->getColumnForType()
#2 /home/servpcspa/web/servpcspa.com/public_html/vendor/robmorgan/phinx/src/Phinx/Db/Table.php(310): Phinx\Db\Adapter\AdapterWrapper->getColumnForType()
#3 /home/servpcspa/web/servpcspa.com/public_html/db/migrations/20240831164252_create_deudores_table.php(17): Phinx\Db\Table->addColumn()
#4 /home/servpcspa/web/servpcspa.com/public_html/vendor/robmorgan/phinx/src/Phinx/Migration/Manager/Environment.php(109): CreateDeudoresTable->change()
#5 /home/servpcspa/web/servpcspa.com/public_html/vendor/robmorgan/phinx/src/Phinx/Migration/Manager.php(389): Phinx\Migration\Manager\Environment->executeMigration()
#6 /home/servpcspa/web/servpcspa.com/public_html/vendor/robmorgan/phinx/src/Phinx/Migration/Manager.php(360): Phinx\Migration\Manager->executeMigration()
#7 /home/servpcspa/web/servpcspa.com/public_html/vendor/robmorgan/phinx/src/Phinx/Console/Command/Migrate.php(124): Phinx\Migration\Manager->migrate()
#8 /home/servpcspa/web/servpcspa.com/public_html/vendor/symfony/console/Command/Command.php(279): Phinx\Console\Command\Migrate->execute()
#9 /home/servpcspa/web/servpcspa.com/public_html/vendor/symfony/console/Application.php(1029): Symfony\Component\Console\Command\Command->run()
#10 /home/servpcspa/web/servpcspa.com/public_html/vendor/symfony/console/Application.php(316): Symfony\Component\Console\Application->doRunCommand()
#11 /home/servpcspa/web/servpcspa.com/public_html/vendor/robmorgan/phinx/src/Phinx/Console/PhinxApplication.php(75): Symfony\Component\Console\Application->doRun()
#12 /home/servpcspa/web/servpcspa.com/public_html/vendor/symfony/console/Application.php(167): Phinx\Console\PhinxApplication->doRun()
#13 /home/servpcspa/web/servpcspa.com/public_html/vendor/robmorgan/phinx/bin/phinx(28): Symfony\Component\Console\Application->run()
#14 /home/servpcspa/web/servpcspa.com/public_html/vendor/bin/phinx(119): include('...')
#15 {main}
A quick Google shows that unsigned is, at best, deprecated in MySQL now, so just drop the 'signed' => false
option.