作者eliang ()
看板Ruby
标题[问题] Rails 建立资料库表格的问题
时间Thu Feb 5 21:17:28 2009
我现在在学 Rails, 版本是 2.2,
想请问一下, 当我们要用 Rails 帮我们建表格时,
通常会藉助 Migration ,
步骤上, 首先需编辑一个 .rb 档, 写出这样的程式码:
class CreateProducts < ActiveRecord::Migration
def self.up
create_table :products do |t|
t.string :title
t.text :description
t.timestamps
end
.......
执行 rake db:mirgrate 之後,
Rails 会自动帮我们建出一个具有 title, descrition 栏位的表格,
但是我发现建出来表格都会有一个 id 栏位, auto_increment 且为 primary key,
假如我今天想舍弃 auto_increment 的 id 栏位,
改以 title 栏位做为 primary key, 要怎麽办?
又我们常会为表格设定索引 (unique key 或 index key) 以加快查询速度,
在 Rails 的 migration 里又要怎麽做?
谢谢!
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 61.224.53.121
※ 编辑: eliang 来自: 61.224.53.121 (02/05 21:18)
1F:推 godfat:我会强烈推荐用datamapper来maintain :o r migrate sucks.. 02/05 22:55