@yasu_2244

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

User.createができない

解決したいこと

Railsチュートリアル第6章「6.4 最後に」のところで
本番環境でUserモデルを使うためには、heroku runコマンドを使ってHeroku上でもマイグレーションを走らせる必要があるとのことなので、本文通り

$ rails test
$ git push heroku
$ heroku run rails db:migrate

その後、本番環境のコンソールに接続することで確認するところでエラー出ました。
エラーを読んでみましたが「"users"は存在しない」ということだけは分かりました。
原因と解決方法が知りたいです。

発生している問題・エラー

$ heroku run rails console --sandbox
Running rails console --sandbox on ⬢ rails---test... up, run.6603 (Free)
D, [2022-01-29T15:27:21.585015 #4] DEBUG -- :    (0.8ms)  BEGIN
Loading production environment in sandbox (Rails 5.1.6)
Any modifications you make will be rolled back on exit

irb(main):001:0> User.create(name: "Michael Hartl", email: "michael@example.com", password: "foobar", password_confirmation: "foobar")
Traceback (most recent call last):
        1: from (irb):1
ActiveRecord::StatementInvalid (PG::UndefinedTable: ERROR:  relation "users" does not exist)
LINE 8:                WHERE a.attrelid = '"users"'::regclass
                                          ^
:               SELECT a.attname, format_type(a.atttypid, a.atttypmod),
                     pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod,
                     c.collname, col_description(a.attrelid, a.attnum) AS comment
                FROM pg_attribute a
                LEFT JOIN pg_attrdef d ON a.attrelid = d.adrelid AND a.attnum = d.adnum
                LEFT JOIN pg_type t ON a.atttypid = t.oid
                LEFT JOIN pg_collation c ON a.attcollation = c.oid AND a.attcollation <> t.typcollation
               WHERE a.attrelid = '"users"'::regclass
                 AND a.attnum > 0 AND NOT a.attisdropped
               ORDER BY a.attnum
0 likes

1Answer

GemfileにRubyのバージョンが書いてなかったので追加。
$bundle install
$git add Gemfile Gemfile.lock
$git commit -m "Write Ruby Version in Gemfile"
$git push
$git push heroku master
で解決しました。

0Like

Your answer might help someone💌