【问题标题】:Failure to Create Table on Heroku -- Migration works locally but fails on Heroku在 Heroku 上创建表失败——迁移在本地工作,但在 Heroku 上失败
【发布时间】:2014-05-11 16:42:12
【问题描述】:

我正在开发 Rails 3 应用程序并尝试运行迁移。我正在尝试创建一个名为 Songs 的表,并且我能够成功地创建该表并在本地迁移。但是,当我将代码推送到 Heroku 并迁移到那里时,我遇到的错误是:

** Execute db:migrate
==  CreateSongs: migrating ====================================================
-- create_table(:songs)
NOTICE:  CREATE TABLE will create implicit sequence "songs_id_seq1" for serial column "songs.id"
rake aborted!
An error has occurred, this and all later migrations canceled:

PG::DuplicateTable: ERROR:  relation "songs" already exists
: CREATE TABLE "songs" ("id" serial primary key, "solo_cello" character varying(255), "created_at" timestamp, "updated_at" timestamp) /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.1.12/lib/active_record/connection_adapters/postgresql_adapter.rb:605:in `async_exec'

我意识到这个错误源于数据库中已经存在的表,但是当我运行时

 heroku run "bundle exec rake db:schema:dump && cat db/schema.rb" 

我看到架构版本位于 ActiveRecord::Schema.define(:version => 20130915155113) 并且在此迁移之后发生了创建 Songs 模型的迁移(它是 2014050604381​​7)。

这是我的本地架构的截断版本:

 ActiveRecord::Schema.define(:version => 20140511155456) do
 [other tables...] 
create_table "songs", :force => true do |t|
  t.string   "solo_cello"
  t.datetime "created_at"
  t.datetime "updated_at"
  t.string   "solo_violin"
  t.string   "duo_one"
  t.string   "duo_two"
  t.string   "trio_a_one"
  t.string   "trio_a_two"
  t.string   "trio_a_three"
  t.string   "trio_b_one"
  t.string   "trio_b_two"
  t.string   "trio_b_three"
  t.string   "quartet_one"
  t.string   "quartet_two"
  t.string   "quartet_three"
  t.string   "quartet_four"
end

这是我的问题:

  1. 如果创建 Song 表 (2014050604381​​7) 的迁移稍后出现,如果数据库上的架构为 20130915155113 版本,那么该表如何存在?

  2. 我需要做什么才能成功迁移?我意识到我可以删除数据库(这是暂存环境),但非常希望不在生产环境中这样做。

  3. 为什么迁移在我的本地机器上运行良好,但在 Heroku 上却不行?

【问题讨论】:

  • 导航到您的应用程序文件夹并运行heroku pg:psql 并在连接后列出带有\dtt 的表并在您的歌曲表之后查看输出,然后使用\dts 列出序列。
  • 谢谢——我运行了这些命令并查看了表格。如果架构版本指向在 Songs 表创建之前的迁移,仍然不确定为什么该表存在。
  • 您是否有机会恢复 Heroku 上尚不存在歌曲表的数据库的先前备份?它将覆盖现有表的内容,但保留歌曲表,除非您明确重新创建数据库。
  • @fivedigit,很好的假设。是的,我几天前删除了临时表并备份了生产数据的备份。您可能会为接下来的步骤推荐什么?

标签: ruby-on-rails ruby ruby-on-rails-3 postgresql heroku


【解决方案1】:

听起来您的歌曲表可能由于恢复数据库备份而被遗漏了。在 Heroku 上恢复数据库备份只会重写备份中存在的表,但不会删除创建备份时尚不存在的表。

有几个步骤可以解决这个问题:

  1. 放下桌子。您可以通过在终端中运行heroku pg:psql 手动删除表,然后执行查询DROP TABLE songs;。如果这是唯一有问题的表,您现在可以运行迁移。

  2. 删除数据库。如果您有数据库备份并且可以承受一些停机时间,您还可以通过从终端运行 heroku pg:reset,然后运行 ​​restoring your database backup 并运行迁移来删除数据库。

【讨论】:

  • 删除歌曲表解决了这个问题。
猜你喜欢
  • 2019-05-12
  • 2016-12-27
  • 1970-01-01
  • 2012-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-31
  • 2021-04-16
相关资源
最近更新 更多