相关文章推荐
不开心的椰子  ·  海康威视相机RGB ...·  9 月前    · 
豪气的小摩托  ·  Spring ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

ActiveRecord::StatementInvalid (PG::UndefinedTable: ERROR: relation "wikis" does not exist

Ask Question

Rails 4.2.5, Ruby 2.3.8, Devise, Heroku 7.16.7

App works great locally and deploys successfully to Heroku. When I visit the live app the home page populates as expected and the about page is accessible. I have a wikis index link and when I try to access it I get the wiki error below. Signing in is not required to view the index page.

I get the same error when I click on the sign-in or sign-up links. Schema shows a table created for both models.

Things I have tried:

  • Deploying from a test branch after commenting out devise_for: users, pushing to Heroku so the migrations would run, subsequently uncomment devise_for: users and pushing agin to Heroku. No luck.

  • I tried rake db:reset. No luck.

  • I looked for and did not find any reference to either Rails model in my factories files.

  • RAILS_ENV=test heroku rake db:migrate

  • WIKI ERROR

    2018-12-13T00:08:10.414883+00:00 app[web.1]: ActiveRecord::StatementInvalid (PG::UndefinedTable: ERROR:  relation "wikis" does not exist
    2018-12-13T00:08:10.414885+00:00 app[web.1]: LINE 1: SELECT "wikis".* FROM "wikis"  ORDER BY created_at DESC
    2018-12-13T00:08:10.414887+00:00 app[web.1]:                               ^
    2018-12-13T00:08:10.414888+00:00 app[web.1]: : SELECT "wikis".* FROM "wikis"  ORDER BY created_at DESC):
    2018-12-13T00:08:10.414890+00:00 app[web.1]:   app/policies/wiki_policy.rb:42:in `resolve'
    2018-12-13T00:08:10.414892+00:00 app[web.1]:   app/controllers/wikis_controller.rb:26:in `index'
    2018-12-13T00:08:10.414893+00:00 app[web.1]: 
    2018-12-13T00:08:10.414895+00:00 app[web.1]: 
    

    Sign-In / Sign-Up Error

    2018-12-13T00:09:49.468548+00:00 app[web.1]: ActiveRecord::StatementInvalid (PG::UndefinedTable: ERROR:  relation "users" does not exist
    2018-12-13T00:09:49.468550+00:00 app[web.1]: LINE 5:                WHERE a.attrelid = '"users"'::regclass
    2018-12-13T00:09:49.468552+00:00 app[web.1]:                                           ^
    2018-12-13T00:09:49.468553+00:00 app[web.1]: :               SELECT a.attname, format_type(a.atttypid, a.atttypmod),
    2018-12-13T00:09:49.468555+00:00 app[web.1]:                      pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
    2018-12-13T00:09:49.468557+00:00 app[web.1]:                 FROM pg_attribute a LEFT JOIN pg_attrdef d
    2018-12-13T00:09:49.468558+00:00 app[web.1]:                   ON a.attrelid = d.adrelid AND a.attnum = d.adnum
    2018-12-13T00:09:49.468560+00:00 app[web.1]:                WHERE a.attrelid = '"users"'::regclass
    2018-12-13T00:09:49.468561+00:00 app[web.1]:                  AND a.attnum > 0 AND NOT a.attisdropped
    2018-12-13T00:09:49.468564+00:00 app[web.1]:                ORDER BY a.attnum
    2018-12-13T00:09:49.468565+00:00 app[web.1]: ):
    

    spec/factories/wikis.rb

    FactoryGirl.define do
      factory :wiki do
        title "MyString"
        body "MyText"
        private false
    

    spec/factories/users.rb

    FactoryGirl.define do
      factory :user do
        email 's[email protected]'
        password 'sample_password'
    

    I would really appreciate some help with this.

    Thank you in advance!

    The following set of statements about your deployment situation assumes a normally-configured rails app.

    Your project is dealing with three separate deployments, and each deployment has its own database (and hence its own database migration state). Your three environments are (labels arbitrary):

  • "test": Runs on your local machine, with a local database, using RAILS_ENV=test in the command line or set implicitly by the command such as "rake test"
  • "development": Runs on your local machine, with a local database (separate than your local test database, as you can see by viewing database/config.yml)
  • "heroku": Run on Heroku's servers, with a database on Heroku's servers
  • Here's how you run the migrations in each of these separate deployments:

  • "test": RAILS_ENV=test rake db:migrate (environment variable configured Rails, and then Rails.env is used to select a configuration from database.yml)
  • "development": rake db:migrate ("development" is the default Rails environment)
  • "heroku": heroku run rake db:migrate <---- Probably your solution.
  • My best guess is that you need to run that last command in order to ensure the migrations have been applied (including the migration called CreateWikis) to the database in your "heroku" deployment.

    Many thanks to all of you for providing valuable input. Learning and relearning everyday. @Luke Griffiths your hunch was spot on. Executing heroku run rake db:migrate did the job. For future readers in my situation: run heroku config to ensure your db is set. You should see DATABASE_URL : postgres://……. Once confirmed execute the above mentioned command. Voila! – Tyrant Davis Dec 13, 2018 at 16:18

    If it runs locally, and you receive the error PG::UndefinedTable this seems to be a missing migration.

    For 2. you can add a Procfile (no extension) to your app root directory and simply put:

    web: bundle exec puma -p $PORT -e $RAILS_ENV release: bundle exec rails db:migrate

    this will automatically run your migrations on deploy, see here: https://devcenter.heroku.com/articles/release-phase

    Just deploying to Heroku will not migrate your database automatically. I would not try to manually log into the Heroku console to run the migrations. You will forget to do that at some point, and bang your head on the error messages :-)

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.