【问题标题】:PG::UndefinedColumn: ERROR: column "image_file_name" of relation "articles" does not existPG :: UndefinedColumn:错误:关系“文章”的列“image_file_name”不存在
【发布时间】:2017-01-30 20:10:44
【问题描述】:

有人可以帮我运行heroku run db:migrate吗?我忘了在 heroku 上运行 db:migration 以及在 dev env 上运行迁移。我做了几个,现在我收到以下错误:

wozane:~/workspace (master) $ heroku run rake db:migrate                           
Running rake db:migrate on ⬢ wozane... up, run.7786
   (0.8ms)  SELECT pg_try_advisory_lock(96974639112725850);
  ActiveRecord::SchemaMigration Load (1.1ms)  SELECT "schema_migrations".* FROM "schema_migrations"
Migrating to RemoveColumnImage (20160917131520)
   (0.7ms)  BEGIN
== 20160917131520 RemoveColumnImage: migrating ================================
-- remove_column(:articles, :image_file_name, :string)
   (1.5ms)  ALTER TABLE "articles" DROP "image_file_name"
   (0.7ms)  ROLLBACK
   (0.8ms)  SELECT pg_advisory_unlock(96974639112725850)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

PG::UndefinedColumn: ERROR:  column "image_file_name" of relation "articles" does not exist
: ALTER TABLE "articles" DROP "image_file_name"

情况是该列已被删除,不存在。

错误消息中提到的迁移(编号 20160917131520):

class RemoveColumnImage < ActiveRecord::Migration[5.0]
  def change
    remove_column :articles, :image_file_name , :string
    remove_column :articles, :image_content_type, :string
    remove_column :articles, :image_file_size, :integer
    remove_column :articles, :image_updated_at, :datetime
  end
end

Schema.rb 看起来像这样:

ActiveRecord::Schema.define(version: 20160921115118) do

  create_table "articles", force: :cascade do |t|
    t.string   "title"
    t.text     "text"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string   "img_url"
  end

  create_table "photos", force: :cascade do |t|
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string   "title"
    t.string   "img_url"
    t.text     "text"
  end

end

我尝试#迁移中的整个代码,但它对那些不起作用的 cmets 没有帮助:

rails db:environment:set RAILS_ENV=production
heroku run rake db:reset 
heroku run rake db:migrate -app wozane
heroku pg:reset DATABASE --confirm wozane

有人知道如何在我的情况下运行 heroku 迁移吗?

提前致谢。

【问题讨论】:

  • 您的文章表似乎没有您要删除的任何列。
  • 我在之前的迁移中删除了它。问题是我当时没有做heroku run rake db:migrate,可能中间发生了太多其他变化。

标签: ruby-on-rails postgresql heroku database-migration heroku-postgres


【解决方案1】:

只需注释掉删除列迁移的代码。

class RemoveColumnImage < ActiveRecord::Migration[5.0]
  def change
    #remove_column :articles, :image_file_name , :string
    #remove_column :articles, :image_content_type, :string
    #remove_column :articles, :image_file_size, :integer
    #remove_column :articles, :image_updated_at, :datetime
  end
end

并尝试运行 heroku run rake db:migrate -app wozane

【讨论】:

  • 迁移到 RemoveColumnImage (20160917131520) (0.7ms) BEGIN == 20160917131520 RemoveColumnImage: 迁移 -- remove_column(:articles, :image_file_name, :string) (5.1ms) ALTER TABLE "articles" DROP "image_file_name " (1.0ms) ROLLBACK (3.5ms) SELECT pg_advisory_unlock(96974639112725850) rake 中止! StandardError:发生错误,此迁移和所有后续迁移已取消:PG::UndefinedColumn:错误:关系“articles”的列“image_file_name”不存在:ALTER TABLE “articles” DROP “image_file_name”
  • 您是否只评论了 remove_column 行? @Wozane 还要检查迁移的名称是否包含版本号 20160917131520?
  • 我只评论了那一行和整个迁移。上面附上完整的迁移编号 20160917131520。
  • 那么您注释的代码行怎么可能在 heroku 上运行。请确认您已在 heroku 上添加了注释的代码行。
  • 只需在本地系统中注释您的代码并将更改后的代码推送到 heorku,然后在 heorku 上运行迁移。
猜你喜欢
  • 2016-01-23
  • 2015-10-13
  • 2013-12-15
  • 2015-07-18
  • 1970-01-01
  • 1970-01-01
  • 2016-06-01
  • 2020-01-16
  • 2016-07-11
相关资源
最近更新 更多