There are charts of what Rails migration types (:string, :integer, :boolean, ...) map to mysql types (varchar(255), int(11), tinyint(1)...), but sometimes you want something not on the chart. For example, for string types, you only get varchar and text, but sometimes you want a char. In mysql, accessing tables with fixed length rows is faster than tables with variable length rows (of course the tradeoff is that if your string is actually variable length you can waste space.

It’s surprisingly simple. If Rails doesn’t understand the column type, it’ll pass it straight through to the database. So if you want a char instead of varchar, just replace:

t.column :token, :string

With:

t.column :token, "char(255)"

(Of course, this may or may not make your migrations non-portable to another database).

1 Response to “Database specific types in Rails migrations”

  1. Joe Says:
    Thanks for blogging about this, I was going to after we discovered it but forgot. That string with the column type can also contain more info, eg: "int(11) unsigned not null"

Sorry, comments are closed for this article.