Migration column type... | Converts to MySQL field type... | Available options1 |
:binary | TINYBLOB, BLOB, MEDIUMBLOB, or LONGBLOB2 | :limit => 1 to 4294967296 (default = 65536)2 |
:boolean | TINYINT(1) | - |
:date | DATE | - |
:datetime | DATETIME | - |
:decimal | DECIMAL | :precision => 1 to 63 (default = 10) :scale => 0 to 30 (default = 0)3 |
:float | FLOAT | - |
:integer | INT | :limit => 1 to 11 (default = 11) |
:primary_key | INT(11) AUTO_INCREMENT PRIMARY KEY | - |
:string | VARCHAR | :limit => 1 to 255 (default = 255) |
:text | TINYTEXT, TEXT, MEDIUMTEXT, or LONGTEXT2 | :limit => 1 to 4294967296 (default = 65536)2 |
:time | TIME | - |
:timestamp | DATETIME | - |
以下为例:
def self.up
create_table :people do |t|
t.column :title, :string
t.column :first_name, :string, :null => false
t.column :last_name, :string, :null => false
t.column :email, :string, :limit => 100, :null => false
t.column :telephone, :string, :limit => 50
t.column :mobile_phone, :string, :limit => 50
t.column :job_title, :string
t.column :date_of_birth, :date
t.column :gender, :string, :limit => 1
t.column :keywords, :string
t.column :notes, :text
t.column :address_id, :integer
t.column :company_id, :integer
t.column :created_at, :timestamp
t.column :updated_at, :timestamp
end
end
1. 默认会产生一个id列
2.:null => false 限制列不能为空
3. address_id, company_id说明与address表和company表有关系
4. created_at 和 updated_at,会在数据创建和更新的时候自动记录时间
* _on : 当记录创建或更新时, 自动记录当前时间. 产生一个date类型的列。
* _at : 当记录创建时,自动记录当前日期和时间,产生一个timestamp 类 型的列.
5.:default, 默认值
参考: http://www.packtpub.com/article/Working-with-Rails-ActiveRecord-Migrations-Models-Scaffolding-and-Database-Completion