【问题标题】:Heroku Drop Table Rails HelpHeroku Drop Table Rails 帮助
【发布时间】:2011-10-21 08:16:50
【问题描述】:

我正在使用 Ruby on Rails,我不再需要我的表 Order,所以我使用 SQLite 管理器将其删除。如何在 heroku 中进行表删除?

编辑 我收到了错误

db/migrate/20110806052256_droptableorders.rb:10: syntax error, unexpected keyword_end, expecting $end

当我运行命令时

class DropTableOrder < ActiveRecord::Migration
  self.up
      drop_table :orders
  end

  self.down
      raise IrreversibleMigration
  end
end

【问题讨论】:

    标签: ruby-on-rails ruby database heroku


    【解决方案1】:

    执行以下命令。这里'abc'是应用名称

    heroku run console --app abc
    

    然后使用,

    ActiveRecord::Migration.drop_table(:orders)
    

    它将删除表“订单”。

    【讨论】:

      【解决方案2】:

      如果您不想创建迁移以删除表并且无法回滚以前的迁移,因为您不想丢失迁移后创建的表中的数据,您可以在 heroku 控制台上使用以下命令删除一个表:

      $ heroku console
      Ruby console for heroku-project-name
      >> ActiveRecord::Migration.drop_table(:orders)
      

      上述命令将从您的 heroku 数据库中删除该表。您可以使用 ActiveRecord::Migration 模块中的 create_table、a​​dd_column、add_index 等其他方法来操作数据库,而无需创建和运行迁移。但请注意,这会在 Rails 创建的用于管理迁移版本的 schema_migrations 表中留下一团糟。

      这仅在您的应用程序仍在开发中并且您不想丢失您在 heroku 上的远程登台服务器上添加的数据时才有用。

      【讨论】:

      • 当我在 dev 中并且正在推动/拉动 db 并且不知何故将其搞砸并且迁移正在消亡时,我发现这很有用。这个命令就像一个魅力,谢谢!
      【解决方案3】:

      只需像这样创建迁移:

      def self.up
          drop_table :orders
      end
      
      def self.down
          # whatever you need to recreate the table or
          # raise IrreversibleMigration
          # if you want this to be irreversible.
      end
      

      然后在下次推送更改时发送heroku rake db:migrate

      您可能希望在 SQLite 中重新创建表,以便您也可以在本地运行此迁移。

      【讨论】:

      • 我遇到了一个错误。 == DropTablesMigration: 迁移 ============================================- - drop_table(:order) rake 中止!发生错误,此迁移和所有后续迁移均已取消:PGError: ERROR: table "order" does not exist : DROP TABLE "order"。但我的餐桌顺序仍然存在
      • @Jake:对不起,错字,应该是:orders 而不是:order
      • 我在本地服务器上重新创建了表 order 并尝试使用您的命令将其删除。你能在顶部查看我所做的编辑吗
      • @Jake:对不起(再次),我今晚似乎有很多错别字。缺少def 已添加。
      • @Jake:不,3.1 与我的大脑和手指不相互交流无关 :)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-04
      • 1970-01-01
      • 2011-07-18
      • 1970-01-01
      • 1970-01-01
      • 2011-02-11
      • 1970-01-01
      相关资源
      最近更新 更多