2011-07-13 66 views

回答

2

只需使用#execute即可在您的遷移中使用您需要的SQL。

execute "ALTER TABLE things MODIFY id UNSIGNED(10) NOT NULL AUTO_INCREMENT" 

或者列尚不存在於所有:

execute "ALTER TABLE things ADD COLUMN id UNSIGNED(10) NOT NULL AUTO_INCREMENT PRIMARY KEY" 

這應該很好地工作。我認爲在你的遷移中可以下降到純SQL,在很多情況下這是必要的。

2

我計算出這個

create_table things, :id => false do |t| 
    t.column :id, 'INT UNSIGNED NOT NULL AUTO_INCREMENT, PRIMARY KEY (id)' 
    t.string :name 
    ... 

OR

create_table things, :id => false do |t| 
    t.column :id, ID_COLUMN 
    t.string :name 
    ... 

其中一些配置/模塊定義,如果你喜歡,以避免自定義SQL其他遷移使用,也

4

ID_COLUMN在遷移中,這也將起作用:

create_table(:user, id: false, primary_key: :id) do |t| 
    t.primary_key :id, :unsigned_integer, null: false, default: :null, auto_increment: true 
    t.string :name 
end