Categorized: Database, Rails

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?

Sources

Comments

Jim → December 10th, 2006 at 9:23 am

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.

Trey → December 10th, 2006 at 9:44 am

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.

Jake Dust → December 23rd, 2006 at 10:54 am

Datetime includes year; Timestamp doesn’t. That’s all the difference, as far as I remember.

Tiago Pinto → January 19th, 2007 at 12:37 pm

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

kofno → February 8th, 2007 at 2:35 pm

I think :decimal is also supported with :precision and :scale options.

Simon → February 14th, 2007 at 9:22 am

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 [...]

Chrononaut → March 5th, 2007 at 1:36 pm

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

Michael Deck → November 19th, 2007 at 2:01 pm

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.

Noah Magram → February 11th, 2008 at 5:34 pm

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.

SEO G → March 6th, 2008 at 8:23 pm

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

ynw → June 7th, 2008 at 8:43 pm

Informations in this post is outdated

What do you think about that?

Elsewhere in the empire: Home, Blog, APOD