我現在在學 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)
推
02/05 22:55, , 1F
02/05 22:55, 1F