1

我有一个最初在 rail 3 中编写的 rails 应用程序,大约一个月前升级到 rails 3.1.10。我刚刚在故事和标签之间创建了一个 habtm 关联,以及相关的数据库迁移。

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)

到目前为止一切顺利,但是 rake 任务只是挂起。. . 用了几个小时。如果我在这里按 ctrl-c 输出到控制台:

^Crake aborted!
An error has occurred, this and all later migrations canceled:

Interrupt: : ROLLBACK
/.rvm/gems/ruby-1.9.2-p290@rails3firstgemset/gems/activerecord-3.1.10/lib/active_record/connection_adapters/postgresql_adapter.rb:605:in `async_exec'
/.rvm/gems/ruby-1.9.2-p290@rails3firstgemset/gems/activerecord-3.1.10/lib/active_record/connection_adapters/postgresql_adapter.rb:605:in `block in execute'
/.rvm/gems/ruby-1.9.2-p290@rails3firstgemset/gems/activerecord-3.1.10/lib/active_record/connection_adapters/abstract_adapter.rb:245:in `block in log'
/.rvm/gems/ruby-1.9.2-p290@rails3firstgemset/gems/activesupport-3.1.10/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
/.rvm/gems/ruby-1.9.2-p290@rails3firstgemset/gems/activerecord-3.1.10/lib/active_record/connection_adapters/abstract_adapter.rb:240:in `log'
/.rvm/gems/ruby-1.9.2-p290@rails3firstgemset/gems/activerecord-3.1.10/lib/active_record/connection_adapters/postgresql_adapter.rb:604:in `execute'
/.rvm/gems/ruby-1.9.2-p290@rails3firstgemset/gems/activerecord-3.1.10/lib/active_record/connection_adapters/postgresql_adapter.rb:664:in `rollback_db_transaction'
/.rvm/gems/ruby-1.9.2-p290@rails3firstgemset/gems/activerecord-3.1.10/lib/active_record/connection_adapters/abstract/database_statements.rb:201:in `rescue in transaction'
/.rvm/gems/ruby-1.9.2-p290@rails3firstgemset/gems/activerecord-3.1.10/lib/active_record/connection_adapters/abstract/database_statements.rb:182:in `transaction'
/.rvm/gems/ruby-1.9.2-p290@rails3firstgemset/gems/activerecord-3.1.10/lib/active_record/transactions.rb:208:in `transaction'
/.rvm/gems/ruby-1.9.2-p290@rails3firstgemset/gems/activerecord-3.1.10/lib/active_record/migration.rb:742:in `ddl_transaction'
/.rvm/gems/ruby-1.9.2-p290@rails3firstgemset/gems/activerecord-3.1.10/lib/active_record/migration.rb:686:in `block in migrate'
/.rvm/gems/ruby-1.9.2-p290@rails3firstgemset/gems/activerecord-3.1.10/lib/active_record/migration.rb:671:in `each'
/.rvm/gems/ruby-1.9.2-p290@rails3firstgemset/gems/activerecord-3.1.10/lib/active_record/migration.rb:671:in `migrate'
/.rvm/gems/ruby-1.9.2-p290@rails3firstgemset/gems/activerecord-3.1.10/lib/active_record/migration.rb:553:in `down'
/.rvm/gems/ruby-1.9.2-p290@rails3firstgemset/gems/activerecord-3.1.10/lib/active_record/migration.rb:627:in `move'
/.rvm/gems/ruby-1.9.2-p290@rails3firstgemset/gems/activerecord-3.1.10/lib/active_record/migration.rb:541:in `rollback'
/.rvm/gems/ruby-1.9.2-p290@rails3firstgemset/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)

有几点需要注意,因为这个应用程序是从 rails 3.0 开始的,所有早期的迁移都使用旧的Class.up Class.down方法来定义迁移。此外,当我第一次遇到挂起时,我将 CreateStoriesTags 迁移切换为使用updown实例方法,但一切仍然挂起。

更新和答案

我注意到 PGAdmin3 Workbench 也挂了。所以,我重新启动了 Mac,现在回滚正在工作。

4

1 回答 1

3

原来是我的数据库服务器有问题。一个简单的重启解决了问题

于 2013-04-05T17:25:31.980 回答