After running a migration I noticed that rails is changing the specified
decimal data type in PostgreSQL. Is there a reason for that?
According to this post:
I found that there is a slight difference between decimal type and
numeric type:
NUMERIC must be exactly as precise as it is defined — so if you define 4
decimal places, the DB must always store 4 decimal places.
DECIMAL must be at least as precise as it is defined. This means that
the database can actually store more digits then specified (due to the
behind-the-scenes storage having space for extra digits). This means the
database might store 1.00005 instead of 1.0000, affecting future
calculations.
class CreateBooks < ActiveRecord::Migration
def change
create_table :books, id: :uuid do |t|
t.decimal :price
t.string :title
t.timestamps
end
end
end
After running this migration, the column price is of numeric type
instead of decimal.
OK, but if they are the same there would be no need for 2 different
types under PostgreSQL database. According to the post link provided,
decimal is more flexible allowing to store less decimal as opposed to
numeric which requires all decimal places to be filled in. This means
that using decimal I could stored the numbers 12345.12345 as well as
12345.12. In the other hand, numeric would only accept 12345.12345 or
12345.12000 if I understood it right.