Ruby-on-rails PGError:错误:关系“table_name”不存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8392899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 02:27:05  来源:igfitidea点击:

PGError: ERROR: relation "table_name" does not exist

ruby-on-railsrubyruby-on-rails-3postgresqlheroku

提问by Richard Burton

I am trying to push a simple app up to heroku and run:

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

heroku rake db:migrate

But I get the following error:

但我收到以下错误:

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)

My migration looks like this:

我的迁移如下所示:

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

And, after referring to another SO answer, I have included the following in my Gemfile:

而且,在参考另一个 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

Thank you in advance for any help!

预先感谢您的任何帮助!

--- UPDATE ---

- - 更新 - -

The main reason I am confused is that this all works locally, just not when I run the migration on heroku.

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

Here is the error I get now:

这是我现在得到的错误:

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

I have been looking at this question:

我一直在看这个问题:

Heroku error when launch rails3.1 app missing postgres gem

启动 rails3.1 应用程序时出现 Heroku 错误,缺少 postgres gem

I am almost-certain my database.yml should not look like this (seeing as I need to be running postgresql!!!):

我几乎可以肯定我的 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

Serious apologies for the nubishness here. Thank you for your help in advance!

对这里的肮脏深表歉意。提前谢谢你的帮助!

Also tried this link: Uploading to Heroku DB rake:migrate problem

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

回答by Robin

create_table :posts

Didn't you forget the s? The table names should be plural.

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

回答by ajbraus

I just ran: bundle exec rake db:migrateand it worked

我刚跑:bundle exec rake db:migrate它奏效了

回答by mobilemonkey

I had a similar problem, but it wasn't caused by the migration or the Gemfile. I had 4 models setup in a polymorphic relationship. Removing the statement

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

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

and removing the subclass' acts_as_*declarations was enough to enable the db:migrateto successfully complete. I then added back the statements in the models and everything worked great. I'm not exactly sure why this is the case, but if you are in a similar situation this may help temporarily until a better solution presents itself.

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

My situation is polymorphic multi-table inheritance scheme between one parent and 3 objects using http://mediumexposure.com/multiple-table-inheritance-active-record/as a baseline.

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

回答by Arcolye

If you're using ActiveAdmin, whichever table PG says doesn't exist, comment out the contents of that ActiveAdmin rb file.

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

For example, for this case PGError: ERROR: relation "posts" does not exist, comment out the entire contents of app/admin/posts.rb, then uncomment after you've done your migrations.

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

回答by Webjedi

Robin probably has it right but just in case...

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

Check the filename/timestamps on your migrations. These get run in sequence. I had an issue where a generator that made my migrations was putting the foreign table first...I switched the file names and it worked.

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

It's a long shot but I thought I'd put that out there.

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

回答by deefour

In my case, I was doing rename_tablein my migration, after having already modified my model name to reflect the new table name. I had moved Userinto User::User. The userstable needed to be renamed to user_users, so my migration looked like

在我的情况下,我正在做rename_table迁移,在已经修改了我的模型名称以反映新表名称之后。我已经搬进UserUser::User。该users表需要重命名为user_users,所以我的迁移看起来像

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

  def down
    rename_table :user_users, :users
  end
end

Instead of

代替

app/models/user.rb

I now had

我现在有

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

with the latter containing the User::Usermodel, and the former containing simply

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

module User
  def self.table_name_prefix
    "user_"
  end
end

It was this class method in the newly added Usermodule that was giving me the PGErrorwhen running rake db:migratelike the OP had. Temporarily removing this class method while I ran my migration allowed the migration to run smoothly.

正是新添加的User模块中的这个类方法给了我像 OP 那样PGError运行rake db:migrate的时间。在我运行迁移时临时删除此类方法允许迁移顺利运行。

回答by mtminogue

I was having the same problem. I ran heroku run rake db:migrateand that solved the problem.

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