【发布时间】: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 模型的迁移(它是 20140506043817)。
这是我的本地架构的截断版本:
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
这是我的问题:
如果创建 Song 表 (20140506043817) 的迁移稍后出现,如果数据库上的架构为 20130915155113 版本,那么该表如何存在?
我需要做什么才能成功迁移?我意识到我可以删除数据库(这是暂存环境),但非常希望不在生产环境中这样做。
为什么迁移在我的本地机器上运行良好,但在 Heroku 上却不行?
【问题讨论】:
-
导航到您的应用程序文件夹并运行
heroku pg:psql并在连接后列出带有\dtt的表并在您的歌曲表之后查看输出,然后使用\dts列出序列。 -
谢谢——我运行了这些命令并查看了表格。如果架构版本指向在 Songs 表创建之前的迁移,仍然不确定为什么该表存在。
-
您是否有机会恢复 Heroku 上尚不存在歌曲表的数据库的先前备份?它将覆盖现有表的内容,但保留歌曲表,除非您明确重新创建数据库。
-
@fivedigit,很好的假设。是的,我几天前删除了临时表并备份了生产数据的备份。您可能会为接下来的步骤推荐什么?
标签: ruby-on-rails ruby ruby-on-rails-3 postgresql heroku