Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Rails' migration equivalent to MySQL's 'double' datatype?

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?

like image 466
JP Silvashy Avatar asked Jun 23 '10 07:06

JP Silvashy


People also ask

What is data migration in Rails?

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.

How Rails db Migrate works?

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.

What is migration in Ruby on Rails?

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.

What is are the default column columns that Rails will generate while migrating?

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).


2 Answers

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
like image 143
Yoshihiro Ashihara Avatar answered Oct 13 '22 19:10

Yoshihiro Ashihara


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.)

like image 22
Andrew Vit Avatar answered Oct 13 '22 21:10

Andrew Vit