2015-06-08 36 views

回答

2

您可以在更改列語句後使用:

def up 
    change_column :your_table, :some_column, :integer, after: :other_column 
end 
+0

THANX第一,但對於存儲在這些字段中的數據? –

+0

我剛剛在Rails 4 w/postgres上測試了它,並將字段從:string更改爲text,或者將它保留爲與之前相同的類型,而不刪除指定字段中的數據 – errata

0

你必須創建一個單獨的遷移更改列。

rails g migration change_data_type_for_fieldname 

class ChangeDataTypeForFieldname < ActiveRecord::Migration 
    def up 
    #change column type form int to bigint 
    change_column :my_table, :my_column, :bigint 
    end 
    def down 
    #change column type form bigint to int 
    change_column :my_table, :my_column, :int 
    end 
end 

請參閱api

相關問題