推送到 Heroku - ActiveRecord::StatementInvalid: PG::UndefinedTable: 错误: 关系“users”不存在

ben*_*l96 2 ruby-on-rails heroku

我现在真的很沮丧。

我在本地有工作 Rails 应用程序,想将其部署到 Heroku。到目前为止,一切都很好。

每当我尝试将代码推送到 Heroku 时,它都会发出未找到表的错误。我确实尝试多次运行 heroku run Rails db:migrate db:drop db:reset 或其他任何命令,但没有任何成功。

development:
  <<: *default
  database: postgres
  encoding: utf8


# 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:
  <<: *default
  adapter: postgresql
  database: fortest

production:
  <<: *default
  adapter: postgresql
  url: <%= ENV['DATABASE_URL'] %>
Run Code Online (Sandbox Code Playgroud)

我的所有表和迁移都在我的本地计算机上正常工作,问题是我需要在 Heroku 上运行这些迁移来创建我的数据库表,但不知何故不能,因为当我的预编译资产启动时它会失败。

    Preparing app for Rails asset pipeline
remote:        Running: rake assets:precompile
remote:        rake aborted!
remote:        ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  relation "users" does not exist
remote:        LINE 8:                WHERE a.attrelid = '"users"'::regclass
remote:                                                  ^
remote:        :               SELECT a.attname, format_type(a.atttypid, a.atttypmod),
remote:                             pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod,
remote:                             c.collname, col_description(a.attrelid, a.attnum) AS comment
remote:                        FROM pg_attribute a
remote:                        LEFT JOIN pg_attrdef d ON a.attrelid = d.adrelid AND a.attnum = d.adnum
remote:                        LEFT JOIN pg_type t ON a.atttypid = t.oid
remote:                        LEFT JOIN pg_collation c ON a.attcollation = c.oid AND a.attcollation <> t.typcollation
remote:                       WHERE a.attrelid = '"users"'::regclass
remote:                         AND a.attnum > 0 AND NOT a.attisdropped
remote:                       ORDER BY a.attnum
Run Code Online (Sandbox Code Playgroud)

编辑:

在我的 Heroku 日志中,它显示“构建失败”——检查构建日志,在我的 buid 日志中,一切正常,直到到达此路径:

 Bundle completed (32.92s)
       Cleaning up the bundler cache.
-----> Installing node-v8.10.0-linux-x64
-----> Detecting rake tasks
-----> Preparing app for Rails asset pipeline
       Running: rake assets:precompile
       rake aborted!
       ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  relation "users" does not exist
       LINE 8:                WHERE a.attrelid = '"users"'::regclass
Run Code Online (Sandbox Code Playgroud)

更新:

Rake::Task["assets:precompile"].clear
namespace :assets do
  task 'precompile' do
    puts "Not pre-compiling assets..."
  end
end
Run Code Online (Sandbox Code Playgroud)

有了这个片段,我至少能够将我的文件推送到heroku。但是,我对所有 db:migrate db:reset 函数仍然存在问题。db:migrate 似乎有相同的错误。

rake aborted!
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  relation "users" does not exist
LINE 8:                WHERE a.attrelid = '"users"'::regclass (...)
Run Code Online (Sandbox Code Playgroud)

db:create 不知何故引发了我这个错误

FATAL:  permission denied for database "postgres"
DETAIL:  User does not have CONNECT privilege.
Run Code Online (Sandbox Code Playgroud)

我看到了所有数据(用户名、密码、适配器、数据库、端口、主机),它们都很好。

Den*_*ler 6

怎么样

$ git push heroku $ heroku run rake db:migrate

然后再次push启动资产预编译。


jmc*_*mcd 5

这里发生的情况是,您devise_for :users的文件中很可能有一个routes.rb,这会导致 Rails 在启动时查找该Users表,但由于您尚未运行迁移,因此无法找到该表。

注释掉devise_for你的routes.rb并推送到 Heroku - 迁移将运行,然后取消注释devise_for并再次推送。

一般来说,最好Users先创建表,然后在稍后的另一个步骤中将设计添加到模型中。