[Rails] ActiveRecord とDBの型の対応関係

Tadashi Shigeoka ·  Thu, October 27, 2011

Ruby on Rails にて、ActiveRecord とDBの型の対応関係を調べたのでご紹介します。

■ PostgreSQL

Ruby 1.9.2 Rails 3.0.10 PostgreSQL 9.0.4

$ rails console
Loading development environment (Rails 3.0.10)
ruby-1.9.2-p290 :001 > pp ActiveRecord::Base.connection.native_database_types
 => {
:primary_key=>"serial primary key", 
:string=>{:name=>"character varying", :limit=>255}, 
:text=>{:name=>"text"}, 
:integer=>{:name=>"integer"}, 
:float=>{:name=>"float"}, 
:decimal=>{:name=>"decimal"}, 
:datetime=>{:name=>"timestamp"}, 
:timestamp=>{:name=>"timestamp"}, 
:time=>{:name=>"time"}, 
:date=>{:name=>"date"}, 
:binary=>{:name=>"bytea"}, 
:boolean=>{:name=>"boolean"}, 
:xml=>{:name=>"xml"}}

■ MySQL

Ruby 1.9.2 Rails 3.0.1 MySQL 5.1.44

ruby-1.9.2-head > pp ActiveRecord::Base.connection.native_database_types
{
:primary_key=>"int(11) DEFAULT NULL auto_increment PRIMARY KEY", 
:string=>{:name=>"varchar", :limit=>255}, 
:text=>{:name=>"text"}, 
:integer=>{:name=>"int", :limit=>4}, 
:float=>{:name=>"float"}, 
:decimal=>{:name=>"decimal"}, 
:datetime=>{:name=>"datetime"}, 
:timestamp=>{:name=>"datetime"},
:time=>{:name=>"time"}, 
:date=>{:name=>"date"},
:binary=>{:name=>"blob"}, 
:boolean=>{:name=>"tinyint", :limit=>1}
}

参考情報