Does anyone know how to create a migration to alter a column type to decimal(10,2)
What is the current schema for that column?
±------------±--------------±-----±----±--------±---------------+
| Field | Type | Null | Key | Default | Extra |
±------------±--------------±-----±----±--------±---------------+
| id | int | NO | PRI | NULL | auto_increment |
| category_id | int | YES | MUL | NULL | |
| name | varchar(255) | NO | | NULL | |
| body | text | NO | | NULL | |
| price | decimal(10,0) | NO | | NULL | |
| image_data | text | YES | | NULL | |
±------------±--------------±-----±----±--------±---------------+
When i try to alter it it changes the 2 to a 0
That should use set_column_type
ROM::SQL.migration do
change do
alter_table :orders do
set_column_type :price, "decimal(10,2)"
end
end
end
Side note: this is not germane to your question, but I’ve spent a lot of time working on billing systems. I would not recommend using a plain decimal(10,2)
for this, although that is better than a floating point.
Although PostgreSQL has monetary types, in my experience the best way to store money is an integer value that gets passed to the Money gem for localization.
Even if you’re not handling multiple currencies, it’s best to build it this way anyway.
Thank you. That should help.