我有一個rails應用程序,我最初在rails 3中編寫,並在一個月前升級到rails 3.1.10。我只是在故事和標籤之間創建了一個habtm關聯,以及相關的數據庫遷移。rake db:最近遷移的回滾掛起
class Story < ActiveRecord::Base
belongs_to :user
has_and_belongs_to_many :tags, :uniq => true
end
class Tag < ActiveRecord::Base
has_and_belongs_to_many :stories, :uniq => true
end
class CreateTags < ActiveRecord::Migration
def change
create_table :tags do |t|
t.string :name
t.timestamps
end
end
end
class CreateStoriesTags < ActiveRecord::Migration
def change
create_table :stories_tags, :id => false do |t|
t.references :story, :tag
end
add_index :stories_tags, [:story_id, :tag_id]
end
end
最後,這裏是有關schema.db:
create_table "stories", :force => true do |t|
t.string "title"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "stories", ["user_id"], :name => "index_stories_on_user_id"
create_table "stories_tags", :id => false, :force => true do |t|
t.integer "story_id"
t.integer "tag_id"
end
add_index "stories_tags", ["story_id", "tag_id"], :name => "index_stories_tags_on_story_id_and_tag_id"
create_table "tags", :force => true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
我遷移了數據庫,然後在控制檯中關聯了這些關聯。現在我想要回滾遷移,以便爲標記表添加一些額外的列。 (我知道我可以簡單地創建一個新的遷移,但我還是想知道爲什麼我現在面臨這個問題)。現在,當我插入rake db:rollback
到控制檯這裏是初始輸出:
[nw0.0.1master (development)]$ rake db:rollback
== CreateStoriesTags: reverting ==============================================
-- drop_table(:stories_tags)
到目前爲止這麼好,但隨後耙子任務就掛起了。 。 。用了幾個小時。如果我CTRL-C出它這裏是輸出到控制檯:
^Crake aborted!
An error has occurred, this and all later migrations canceled:
Interrupt: : ROLLBACK
/.rvm/gems/[email protected]/gems/activerecord-3.1.10/lib/active_record/connection_adapters/postgresql_adapter.rb:605:in `async_exec'
/.rvm/gems/[email protected]/gems/activerecord-3.1.10/lib/active_record/connection_adapters/postgresql_adapter.rb:605:in `block in execute'
/.rvm/gems/[email protected]/gems/activerecord-3.1.10/lib/active_record/connection_adapters/abstract_adapter.rb:245:in `block in log'
/.rvm/gems/[email protected]/gems/activesupport-3.1.10/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
/.rvm/gems/[email protected]/gems/activerecord-3.1.10/lib/active_record/connection_adapters/abstract_adapter.rb:240:in `log'
/.rvm/gems/[email protected]/gems/activerecord-3.1.10/lib/active_record/connection_adapters/postgresql_adapter.rb:604:in `execute'
/.rvm/gems/[email protected]/gems/activerecord-3.1.10/lib/active_record/connection_adapters/postgresql_adapter.rb:664:in `rollback_db_transaction'
/.rvm/gems/[email protected]/gems/activerecord-3.1.10/lib/active_record/connection_adapters/abstract/database_statements.rb:201:in `rescue in transaction'
/.rvm/gems/[email protected]/gems/activerecord-3.1.10/lib/active_record/connection_adapters/abstract/database_statements.rb:182:in `transaction'
/.rvm/gems/[email protected]/gems/activerecord-3.1.10/lib/active_record/transactions.rb:208:in `transaction'
/.rvm/gems/[email protected]/gems/activerecord-3.1.10/lib/active_record/migration.rb:742:in `ddl_transaction'
/.rvm/gems/[email protected]/gems/activerecord-3.1.10/lib/active_record/migration.rb:686:in `block in migrate'
/.rvm/gems/[email protected]/gems/activerecord-3.1.10/lib/active_record/migration.rb:671:in `each'
/.rvm/gems/[email protected]/gems/activerecord-3.1.10/lib/active_record/migration.rb:671:in `migrate'
/.rvm/gems/[email protected]/gems/activerecord-3.1.10/lib/active_record/migration.rb:553:in `down'
/.rvm/gems/[email protected]/gems/activerecord-3.1.10/lib/active_record/migration.rb:627:in `move'
/.rvm/gems/[email protected]/gems/activerecord-3.1.10/lib/active_record/migration.rb:541:in `rollback'
/.rvm/gems/[email protected]/gems/activerecord-3.1.10/lib/active_record/railties/databases.rake:232:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:rollback
(See full trace by running task with --trace)
幾件事情要注意,因爲這個程序開始了W /導軌3.0,所有早期的遷移使用舊Class.up Class.down
方法來定義遷移。另外,當我第一次查看懸掛時,我將CreateStoriesTags遷移切換爲使用up
和down
實例方法,但一切仍然懸掛。
我注意到PGAdmin3 Workbench也掛了。所以,我重新啓動了Mac,現在回滾正在發現。
當遷移文件被遷移後,當遷移文件發生變化時,出現了這個問題,然後嘗試回滾。正如OP所說,在我的情況下重新啓動PG解決了這個問題。 – 2016-07-29 16:39:23