2015-04-24 68 views
1

在我的應用程序,我編輯這裏顯示遷移文件:schema.rb文件沒有更新

class CreateUsers < ActiveRecord::Migration 
    def change 
    create_table :users do |t| 
     t.string :first_name 
     t.string :last_name 
     t.string :dj_alias 
     t.boolean :site_admin 
     t.integer :station_id 
     t.string :byline 
     t.string :bio 

     t.timestamps null: false 
    end 
    end 
end 

有生物和署名領域。但是,當我運行rake db:reset時,schema.rb文件中沒有更改。我看到的唯一錯誤,有這個代碼塊:

ActiveRecord::Base.connection.tables.each do |table| 
    result = ActiveRecord::Base.connection.execute("SELECT id FROM #{table} ORDER BY id DESC LIMIT 1") rescue (puts "Warning: not procesing table #{table}. Id is missing?" ; next) 
    ai_val = result.any? ? result.first['id'].to_i + 1 : 1 
    puts "Resetting auto increment ID for #{table} to #{ai_val}" 

    ActiveRecord::Base.connection.execute("ALTER SEQUENCE #{table}_id_seq RESTART WITH #{ai_val}") 
end 

在seeds.rb文件,其目的是要處理的種子文件的索引的底部。當我運行rake db:reset時,第一行的rescue語句顯示:Warning:not procesing table schema_migrations。編號缺失?

我想我很困惑,爲什麼這句話能拯救這個?儘管看起來可能是原因,但是在訪問seeds.rb文件之前是否發生了schema.rb重置?

這裏是耙分貝的輸出:遷移:狀態

Status Migration ID Migration Name 
-------------------------------------------------- 
up  20150225041954 Create songs 
up  20150225042739 Create albums 
up  20150225043102 Create artists 
up  20150225043854 Create playlists 
up  20150225044118 Create users 
up  20150225044314 Create stations 
up  20150225061259 Create featured artists 
up  20150225153938 Add devise to users 
up  20150225200646 Create reviews 
up  20150321171830 Stations users 
up  20150323200255 Add last fm to album 
up  20150323200432 Add last fm to artist 
up  20150323200513 Add last fm to song 
up  20150325052314 Albums stations 
up  20150325061241 Playlist songs 
up  20150327172516 Add image url to albums 
up  20150327172532 Add image url to artists 

回答

1

對於遷移文件應用的新變化,你需要運行rake db:migrate。如果遷移在您進行更改之前已經運行,請運行rake db:rollback以回滾遷移並再次應用遷移。

rake db:reset不適用遷移文件中的新更改。它試圖加載已經在schema.rb文件中的內容。

請參閱http://edgeguides.rubyonrails.org/active_record_migrations.html#setup-the-database & http://edgeguides.rubyonrails.org/active_record_migrations.html#resetting-the-database有關rake db:reset如何工作的更多詳細信息。

運行rake db:migrate:status查看運行的遷移。

+0

我沒有嘗試過,是否有可能需要運行rake db:rollback多次? –

+0

我在rake db:migrate:status的輸出中編輯了我的原始問題 –

+0

有關如何回滾特定遷移的信息,請參閱http://stackoverflow.com/a/6635407/429758。 –