The information in this post is outdated. You might want to look elsewhere for up-to-date information.
Rails Migration Data Types
Is this all of them?
:integer
:float
:datetime
:date
:timestamp
:time
:text
:string
:binary
:boolean
Found this:
Valid column types are integer, float, datetime, date, timestamp, time, text, string, binary, and boolean. Valid column options are limit, null (i.e. ” :null => false” implies NOT NULL), and default (to specify default values).
Why is this hard to find? Google search for “rails migration data types” isn’t that good.
And what’s the difference between datetime and timestamp? Or binary and boolean? Any database gurus read this?
Comments
If i were forced to guess, I might speculate that a “boolean” is a standard boolean and that “binary” correlates to something like MySQL’s BINARY column type; similar to TEXT, but for binary data–e.g., raw files or something.
Ooooh yeah. Duh. Of course binary == storing files in the DB. I guess I was just stuck on “binary” being 0s and 1s and boolean being either a 0 or a 1.
Datetime includes year; Timestamp doesn’t. That’s all the difference, as far as I remember.
Unless I’m completely wrong and accordingly to what I can remember, I think timestamp includes something related to unix timestamp (like fractions of seconds or something).
I think :decimal is also supported with :precision and :scale options.
Trey:
When you say binary being 0s and 1s, what you are saying is that binary is represented by 0s and 1s in machine language. Then everything in every software is represented by 0s and 1s. Boolean, on the other hand, is represented by 0 or 1 in programming language. That 0 or 1 in turn is represented by a bunch of 0s and 1s.
[...] came across this blog post when I googled rails data type. Below is part of the [...]
Timestamps require half the space of a datetime to store, because they are based on unixtime. Therefore they also have a smaller range (1970-2050 or something).
Migration data types are just aliases for the column types supported by the underlying database. So the answer to your question “what’s the difference between datetime and timestamp” is that it depends on which RDBMS you’re using. In MySql there is no difference, both map to datetime. In Sybase however they map to datetime and timestamp respectivly. In Oracle all of the date types (date, time, datetime, timestamp) all map to the date type.
The Agile Web Development with Rails book has a nice chart that defines the mapping from migration type to db column type for all of the major databases. The chart is in chapter 16, pg 267 of my version of the book.
A TIMESTAMP column in many RDBMSes gets set automagically on every row insert, some on every update. It can also be set explicitly in an INSERT or UPDATE statement.
It also appears that rails’ :binary really does mean BLOB, not BINARY - so much for convention there.
Using Rails 2.0 in the default migrations file it uses t.timestamps and it automatically creates the columns created_at and updated_at (both datetime in MySQL). I guess since so many people wanted to have those columns for most models they rolled it into the standard migrations file when you generate the model file.
Michael
Informations in this post is outdated
What do you think about that?