質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

PostgreSQL

PostgreSQLはオープンソースのオブジェクトリレーショナルデータベース管理システムです。 Oracle Databaseで使われるPL/SQLを参考に実装されたビルトイン言語で、Windows、 Mac、Linux、UNIX、MSなどいくつものプラットフォームに対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

15292閲覧

本番環境のDBエラーに困っています(rails)

yamady

総合スコア176

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

PostgreSQL

PostgreSQLはオープンソースのオブジェクトリレーショナルデータベース管理システムです。 Oracle Databaseで使われるPL/SQLを参考に実装されたビルトイン言語で、Windows、 Mac、Linux、UNIX、MSなどいくつものプラットフォームに対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

1クリップ

投稿2017/05/22 16:44

編集2017/05/22 23:51

###分からないこと・前提

開発環境:Ruby on Rails 5.0.0.1
本番環境DB:PostgreSQL
サーバー:Heroku

フルスクラッチでUserモデル作成から、Devise使用に移行しました。
ある程度、修復したあとに、Herokuへ本番環境にアップしようとしたら下記エラーが発生しました。

コンソールで「heroku run rake db:migrate」後のメッセージです。

Ruby

1PG::UndefinedTable: ERROR: relation "users" does not exist 2 3: CREATE TABLE "reviews" ("id" serial primary key, "title" text, "rate" integer, "content" text, "user_id" integer, "space_id" integer, "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL, CONSTRAINT "fk_rails_74a66bd6c5" 4FOREIGN KEY ("user_id") 5 REFERENCES "users" ("id") 6, CONSTRAINT "fk_rails_ca8b56cfc5" 7FOREIGN KEY ("space_id") 8 REFERENCES "spaces" ("id") 9) 10 11PG::UndefinedTable: ERROR: relation "users" does not exist 12 13ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "users" does not exist 14: CREATE TABLE "reviews" ("id" serial primary key, "title" text, "rate" integer, "content" text, "user_id" integer, "space_id" integer, "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL, CONSTRAINT "fk_rails_74a66bd6c5" 15FOREIGN KEY ("user_id") 16 REFERENCES "users" ("id") 17, CONSTRAINT "fk_rails_ca8b56cfc5" 18FOREIGN KEY ("space_id") 19 REFERENCES "spaces" ("id") 20)

要するに、Userデータベースがないよと言ってくれているんだと思われますが、ローカル環境では問題なく動いています。
ただ、ローカル環境ではSQ Liteを使っています。(よくないですよね、、移行しようとは思いつつもチュートリアルのままに続けていて...)

###試してみたこと

下記の回答にあります通り、「bundle exec rake db:migrate」は試してみました。→ 効果なし
https://teratail.com/questions/23996

こっちにあります通り、rake db:schema:load RAILS_ENV=production→ rake db:migrate RAILS_ENV=productionを試しましたがやはり無知でした。
http://qiita.com/zaqisuke/items/865fc0eed3ad24dbf3f3

こうした場合どうすればよろしいでしょうか。
取り急ぎ、heroku log は下記です。

2017-05-22T16:15:36.818457+00:00 heroku[run.3331]: State changed from starting to up 2017-05-22T16:15:36.758190+00:00 heroku[run.3331]: Awaiting client 2017-05-22T16:15:36.943760+00:00 heroku[run.3331]: Starting process with command `bundle exec rake db:environment:set RAILS_ENV=production` 2017-05-22T16:15:45.466266+00:00 heroku[run.3331]: State changed from up to complete 2017-05-22T16:15:45.450785+00:00 heroku[run.3331]: Process exited with status 0 2017-05-22T16:15:56.197864+00:00 app[api]: Starting process with command `bundle exec rake db:migrate` by user 2017-05-22T16:16:10.514584+00:00 heroku[run.8107]: Awaiting client 2017-05-22T16:16:10.755493+00:00 heroku[run.8107]: State changed from starting to up 2017-05-22T16:16:11.138228+00:00 heroku[run.8107]: Starting process with command `bundle exec rake db:migrate` 2017-05-22T16:16:17.923237+00:00 heroku[run.8107]: State changed from up to complete 2017-05-22T16:16:17.893638+00:00 heroku[run.8107]: Process exited with status 1 2017-05-22T16:23:53.041299+00:00 app[api]: Starting process with command `bundle exec rake db:migrate` by user 2017-05-22T16:24:09.707883+00:00 heroku[run.1793]: Awaiting client 2017-05-22T16:24:09.758816+00:00 heroku[run.1793]: Starting process with command `bundle exec rake db:migrate` 2017-05-22T16:24:09.995355+00:00 heroku[run.1793]: State changed from starting to up 2017-05-22T16:24:19.611596+00:00 heroku[run.1793]: State changed from up to complete 2017-05-22T16:24:19.597841+00:00 heroku[run.1793]: Process exited with status 1

###追記

$heroku run rake db:migrate

FATAL: permission denied for database "postgres"
DETAIL: User does not have CONNECT privilege.

$heroku pg:reset DATABASE

上記2つもうまくいかずです。ローカル環境は動いているのに。。

Ruby

1Rails.application.configure do 2 # Settings specified here will take precedence over those in config/application.rb. 3 4 # Code is not reloaded between requests. 5 config.cache_classes = true 6 7 # Eager load code on boot. This eager loads most of Rails and 8 # your application in memory, allowing both threaded web servers 9 # and those relying on copy on write to perform better. 10 # Rake tasks automatically ignore this option for performance. 11 config.eager_load = true 12 13 # Full error reports are disabled and caching is turned on. 14 config.consider_all_requests_local = false 15 config.action_controller.perform_caching = true 16 17 # Disable serving static files from the `/public` folder by default since 18 # Apache or NGINX already handles this. 19 config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present? 20 21 # Compress JavaScripts and CSS. 22 config.assets.js_compressor = :uglifier 23 # config.assets.css_compressor = :sass 24 25 # Do not fallback to assets pipeline if a precompiled asset is missed. 26 config.assets.compile = false 27 28 # `config.assets.precompile` and `config.assets.version` have moved to config/initializers/assets.rb 29 30 # Enable serving of images, stylesheets, and JavaScripts from an asset server. 31 # config.action_controller.asset_host = 'http://assets.example.com' 32 33 # Specifies the header that your server uses for sending files. 34 # config.action_dispatch.x_sendfile_header = 'X-Sendfile' # for Apache 35 # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX 36 37 # Mount Action Cable outside main process or domain 38 # config.action_cable.mount_path = nil 39 # config.action_cable.url = 'wss://example.com/cable' 40 # config.action_cable.allowed_request_origins = [ 'http://example.com', /http:\/\/example.*/ ] 41 42 # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies. 43 config.force_ssl = false 44 45 # Use the lowest log level to ensure availability of diagnostic information 46 # when problems arise. 47 config.log_level = :debug 48 49 # Prepend all log lines with the following tags. 50 config.log_tags = [ :request_id ] 51 52 # Use a different cache store in production. 53 # config.cache_store = :mem_cache_store 54 55 # Use a real queuing backend for Active Job (and separate queues per environment) 56 # config.active_job.queue_adapter = :resque 57 # config.active_job.queue_name_prefix = "spacehacker_app_#{Rails.env}" 58 config.action_mailer.perform_caching = false 59 60 # Ignore bad email addresses and do not raise email delivery errors. 61 # Set this to true and configure the email server for immediate delivery to raise delivery errors. 62 63 # Enable locale fallbacks for I18n (makes lookups for any locale fall back to 64 # the I18n.default_locale when a translation cannot be found). 65 config.i18n.fallbacks = true 66 67 # Send deprecation notices to registered listeners. 68 config.active_support.deprecation = :notify 69 70 # Use default logging formatter so that PID and timestamp are not suppressed. 71 config.log_formatter = ::Logger::Formatter.new 72 73 # Use a different logger for distributed setups. 74 # require 'syslog/logger' 75 # config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new 'app-name') 76 77 if ENV["RAILS_LOG_TO_STDOUT"].present? 78 logger = ActiveSupport::Logger.new(STDOUT) 79 logger.formatter = config.log_formatter 80 config.logger = ActiveSupport::TaggedLogging.new(logger) 81 end 82 83 # Do not dump schema after migrations. 84 config.active_record.dump_schema_after_migration = false 85end 86

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

users tableが先に作られていないことによるエラーではないでしょうか。
マイグレーションファイルからdeviceでuserを作るマイグレーションファイルの
名前が先頭にくるようにリネームして(マイグレーションはファイル名順に行われます)
例えば先頭に0を追加して
0000filename
再度
rake db:migrate RAILS_ENV=production
としてみてはいかがでしょう。

投稿2017/05/23 00:15

編集2017/05/23 00:24
moke

総合スコア2241

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

yamady

2017/05/23 01:06

mokeさま、いつも本当にありがとうございます。 Railsは読み込みの順番が大切だとよく聞いておりました。今回、000filenameでやってみたところ、うまくいきました。助かります!
moke

2017/05/23 01:15 編集

よかったです。 Rails(レールズ)ですから、他の言語より、はるかに同じところで つまづくんですよねー。 的確なサポートがあれば、これ以上上達の早い言語はないと思います。
yamady

2017/05/23 02:51

ありがとうございます! 普段はHTML / CSSを少し書いていただけですが、非常に取り掛かりやすい言語でした。また、調べて解決できるだけWebにナレッジが溜まっているのも非常にありがたいかぎりです(泣
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問