【问题标题】:Rails editing migration to ignore an errorRails 编辑迁移以忽略错误
【发布时间】:2015-02-14 17:05:53
【问题描述】:

我有一个包含帖子、用户、标签等的应用程序。我一直在本地处理它,但由于问题而无法将其推送到 heroku。最后,我成功地将我的应用程序推送到 heroku,然后意识到我从未将我的数据库迁移到那里。所以我跑了

heroku run rake db:migrate

得到了这个错误:

== 20141116151429 CreatePosts: migrating ======================================
-- drop_table(:posts)
PG::UndefinedTable: ERROR:  table "posts" does not exist
: DROP TABLE "posts"
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

PG::UndefinedTable: ERROR:  table "posts" does not exist

我查看了迁移,由于某种原因,它在其他任何东西之前都有 drop tables 行:

class CreatePosts < ActiveRecord::Migration
  def change
    drop_table :posts
    create_table :posts do |t|
      t.string :title,              null: false, default: ""
      t.text :description, null: false, default: ""

      t.timestamps
    end
  end
end

我注释掉了 drop table 行,甚至删除了它,然后使用 git commit 提交它,然后尝试运行 heroku rake db:migrate,但错误仍然显示。我知道这是一个严重的混乱,但我不知道从这里该怎么做。

我没有尝试重置数据库,因为担心可能会出现问题,尽管从技术上讲,我可以丢失我迄今为止创建的帖子/用户/cmets。

有没有办法可以解决这个问题,并且仍然将我所有的数据库数据推送到 heroku?如果没有,我应该怎么做才能让应用首先运行?

编辑:

我什至将迁移更改为:

class CreatePosts < ActiveRecord::Migration
  def up
    create_table :posts do |t|
      t.string :title,              null: false, default: ""
      t.text :description, null: false, default: ""

      t.timestamps
    end
  end

  def down
    drop_table :posts
  end
end

但错误仍然显示。我不明白:我知道我编辑了正确的文件 (20141116151429_create_posts.rb) 并删除了该行。甚至更改了它的全部内容并提交了这些更改,但是运行 heroku rake db:migrate 仍然会出现 drop_tables 错误。

据我了解,heroku rake db:migrate 与 rake db:migrate 不一样吗?如果是这样,那么它应该运行待处理的迁移。在这种情况下,所有这些都是,因为我从未迁移到 heroku。如果是这样,那么我对尚未运行的迁移文件所做的任何更改都应该得到反映。然而我得到了这个错误。

【问题讨论】:

  • 你不能做 def down 和做 drop_table。
  • 评论 #drop_table :posts and migrate,它会起作用的......
  • 我确实注释掉了 drop_table - 似乎是最合乎逻辑的反应。我什至删除了该行并运行了迁移,但错误仍然显示。
  • 好的,将 def 名称重命名为“def up”并评论 #drop_table :posts,现在尝试....
  • 我尝试将整个结构更改为上面的编辑 - 它仍然显示 -- drop_table(:posts) ...我只是对为什么在我删除它时坚持该行存在感到震惊.有任何想法吗? (顺便说一句,感谢您的快速回复。)

标签: ruby-on-rails ruby heroku


【解决方案1】:

当您注释掉 drop_table 语法时,它应该可以工作。但如果它不起作用,请尝试使用以下语法。

只有在数据库中存在表时才会运行drop_table

class CreatePosts < ActiveRecord::Migration
  def change
    drop_table 'posts' if ActiveRecord::Base.connection.table_exists? 'posts'
    create_table :posts do |t|
      t.string :title,              null: false, default: ""
      t.text :description, null: false, default: ""

      t.timestamps
    end
  end
end

【讨论】:

    【解决方案2】:

    这很尴尬。

    感谢@Dipak 和@RubyOnRails 的快速回复。

    似乎我的提交在第一次提交后完全忽略了迁移文件。我恢复并使用添加的更改进行了干净的提交,并且它使迁移没有任何问题。

    也许我在第一次提交后对 heroku 的提交没有通过。

    尴尬。

    【讨论】:

      【解决方案3】:

      如果你使用'change',drop line就没用了

       # == Reversible Migrations
        #
        # Starting with Rails 3.1, you will be able to define reversible migrations.
        # Reversible migrations are migrations that know how to go +down+ for you.
        # You simply supply the +up+ logic, and the Migration system will figure out
        # how to execute the down commands for you.
        #
        # To define a reversible migration, define the +change+ method in your
        # migration like this:
        #
        #   class TenderloveMigration < ActiveRecord::Migration
        #     def change
        #       create_table(:horses) do |t|
        #         t.column :content, :text
        #         t.column :remind_at, :datetime
        #       end
        #     end
        #   end
        #
         class CreatePosts < ActiveRecord::Migration
            def change
              create_table :posts do |t|
                t.string :title,              null: false, default: ""
                t.text :description, null: false, default: ""
      
                t.timestamps
              end
            end
          end
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-04-21
        • 2013-12-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-17
        • 2011-07-28
        • 2015-04-04
        相关资源
        最近更新 更多