【问题标题】:PGError: ERROR: relation "table_name" does not existPGError:错误:关系“table_name”不存在
【发布时间】:2012-01-13 15:23:13
【问题描述】:

我正在尝试将一个简单的应用程序推送到 heroku 并运行:

heroku rake db:migrate

但我收到以下错误:

rake aborted!
PGError: ERROR:  relation "posts" does not exist
:             SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull
              FROM pg_attribute a LEFT JOIN pg_attrdef d
                ON a.attrelid = d.adrelid AND a.attnum = d.adnum
             WHERE a.attrelid = '"posts"'::regclass
               AND a.attnum > 0 AND NOT a.attisdropped
             ORDER BY a.attnum

Tasks: TOP => db:migrate => environment
(See full trace by running task with --trace)

我的迁移如下所示:

class CreatePosts < ActiveRecord::Migration
  def change
    create_table :posts do |t|
      t.string :source
      t.string :tweetid
      t.string :pure
      t.string :media
      t.string :destination
      t.datetime :time
      t.timestamps
    end
  end
end

并且,在参考了另一个 SO 答案之后,我在我的 Gemfile 中包含了以下内容:

# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails',   '~> 3.1.4'
  gem 'coffee-rails', '~> 3.1.1'
  gem 'uglifier', '>= 1.0.3'
  gem 'pg'
end

提前感谢您的帮助!

--- 更新 ---

我感到困惑的主要原因是这一切都在本地工作,而不是当我在 heroku 上运行迁移时。

这是我现在得到的错误:

rake aborted!
Please install the postgresql adapter: `gem install activerecord-postgresql-adapter` (pg is not part of the bundle. Add it to Gemfile.)

我一直在看这个问题:

Heroku error when launch rails3.1 app missing postgres gem

我几乎可以肯定我的 database.yml 不应该是这样的(因为我需要运行 postgresql !!!):

# SQLite version 3.x
#   gem install sqlite3
#
#   Ensure the SQLite 3 gem is defined in your Gemfile
#   gem 'sqlite3'
development:
  adapter: sqlite3
  database: db/development.sqlite3
  pool: 5
  timeout: 5000

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  adapter: sqlite3
  database: db/test.sqlite3
  pool: 5
  timeout: 5000

production:
  adapter: sqlite3
  database: db/production.sqlite3
  pool: 5
  timeout: 5000

对于此处的不雅内容,我们深表歉意。提前感谢您的帮助!

也试过这个链接:Uploading to Heroku DB rake:migrate problem

【问题讨论】:

  • gem 'pg' 应该在 group :assets 块之外,我认为它与它无关。你试过heroku rake db:setup吗?
  • heroku 的 gemfile 中不需要 pg
  • 抱歉,这是一个较早的实验,看看复数是否影响了它。
  • 有同样的问题.. 不会 rake db:setup 删除所有现有数据?

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


【解决方案1】:

我刚刚跑了:bundle exec rake db:migrate,它成功了

【讨论】:

    【解决方案2】:

    我遇到了同样的问题。我跑了heroku run rake db:migrate,这解决了问题。

    【讨论】:

      【解决方案3】:
      create_table :posts
      

      你没有忘记s 吗?表名应该是复数。

      【讨论】:

        【解决方案4】:

        如果您使用 ActiveAdmin,无论 PG 说哪个表不存在,请注释掉该 ActiveAdmin rb 文件的内容。

        例如,对于本例PGError: ERROR: relation "posts" does not exist,注释掉app/admin/posts.rb 的全部内容,然后在完成迁移后取消注释。

        【讨论】:

        • 谢谢,这是给我的
        • 这对我不起作用。另外,您会注释掉模型文件的哪些部分?
        【解决方案5】:

        在我的例子中,我在我的迁移中做rename_table,在我已经修改了我的模型名称以反映新的表名之后。我已将User 移动到User::Userusers 表需要重命名为 user_users,所以我的迁移看起来像

        class RenameUsersTableWithPrefix < ActiveRecord::Migration
          def up
            rename_table :users, :user_users
          end
        
          def down
            rename_table :user_users, :users
          end
        end
        

        代替

        app/models/user.rb
        

        我现在有

        app/models/user.rb
        app/models/user/user.rb
        

        后者包含User::User 模型,前者包含简单

        module User
          def self.table_name_prefix
            "user_"
          end
        end
        

        正是新添加的User 模块中的这个类方法在像OP 一样运行rake db:migrate 时给了我PGError。在我运行迁移时暂时删除此类方法可以让迁移顺利进行。

        【讨论】:

          【解决方案6】:

          我遇到了类似的问题,但不是由迁移或 Gemfile 引起的。我在多态关系中设置了 4 个模型。删除语句

          belongs_to :assetable, :polymorphic => true, :dependent => :destroy
          

          删除子类的acts_as_* 声明足以使db:migrate 成功完成。然后我在模型中添加了语句,一切都很好。我不确定为什么会这样,但如果您处于类似情况,这可能会暂时有所帮助,直到出现更好的解决方案。

          我的情况是一个父对象和 3 个对象之间的多态多表继承方案,使用 http://mediumexposure.com/multiple-table-inheritance-active-record/ 作为基线。

          【讨论】:

          • 我猜想与多态关联相关的东西是在创建它后面的表之前尝试加载模型类。问题中的 SQL 是 AR 用来确定表模式的。在这种情况下,ActiveAdmin 有时是罪魁祸首。
          • 有趣,我正在使用 ActiveAdmin。到目前为止,我还不知道 ActiveAdmin 可能在其中发挥了作用,尤其是在 db:migration 中。
          • 我很确定 ActiveAdmin 尝试在应用程序初始化程序中预加载一些东西,并且初始化发生在 db:migrate 之前。我不知道 ActiveAdmin 的某些部分或模型关联会触发问题,但人们通过暂时禁用 ActiveAdmin 解决了类似的问题。
          【解决方案7】:

          罗宾可能是对的,但以防万一......

          检查迁移的文件名/时间戳。这些按顺序运行。我遇到了一个问题,使我进行迁移的生成器将外部表放在首位...我切换了文件名并且它起作用了。

          这是一个很长的镜头,但我想我会把它放在那里。

          【讨论】:

            猜你喜欢
            • 2014-01-18
            • 2012-12-20
            • 2011-01-03
            • 2020-09-03
            • 2020-04-15
            • 2011-07-23
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多