I can't seem to figure this one out by searching Google.
I have an existing database which has some columns with the type of double
, what would I write in my rails migration to create an equivalent column type? It's a relatively old database and there maybe a more suitable datatype to use completely, but I wanted to see what others thought.
I was going to use the type of :decimal
, is this the best choice?
Thoughts?
A Rails migration is a tool for changing an application's database schema. Instead of managing SQL scripts, you define database changes in a domain-specific language (DSL). The code is database-independent, so you can easily move your app to a new platform.
When you run db:migrate, rails will check a special table in the database which contains the timestamp of the last migration applied to the database. It will then apply all of the migrations with timestamps after that date and update the database table with the timestamp of the last migration.
Migrations in Ruby on Rails allow us to make changes in the database schema in a convenient manner. Rails migrations help keep track of what new changes are made to an application's database schema, when each change is made, and the order in which the changes are made.
You can append as many column name/type pairs as you want. By default, the generated migration will include t. timestamps (which creates the updated_at and created_at columns that are automatically populated by Active Record).
In my case (for test-db preparation):
MySQL (with mysql2(0.3.11) driver):
double(64,12)
Rails (in db/schema.rb):
t.float :limit=>64 ==> failed
t.float :limit=>53 ==> occasionally succeeded
t.decimal :precision=>64, :scale=>12 ==> fully succeeded
There's no specific type for double, Rails tries to be smart about it:
The numeric (integer/decimal/float) column definitions use the :limit
option for the number of bytes in an integer column type, and the :scale
/:precision
options for floats.
(precision is the number of significant digits; scale is how many of these fall after the decimal point.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With