3

I have a Rails app called "enrollment_app" that initializes and populates all the tables in the database with a MySQL seed file. I built the app, added some migrations and pushed my app to Heroku. However, since Heroku uses Postgres, I need a way to make my MySQL database compatible with Heroku so I am using the ClearDB addon.

When I try to open the app though, I get the message:

Application Error
An error occurred in the application and your page could not be served. Please try again in a few moments.

If you are the application owner, check your logs for details.

So, I checked the logs and see this error:

PG::UndefinedTable: ERROR:  relation "enrollments" does not exist

I've been following along with this tutorial but apparently I don't know how to make ClearDB look like my local MySQL DB since I'm getting that error above. How can I do the equivalent of rake db:seed the MySQL seed file and rake db:migrate to the production ClearDB database?

Updated - Gemfile:

source 'https://rubygems.org'

gem 'rails', '4.2.1'
gem 'mysql2'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'bootstrap-sass', '~> 3.3.5'
gem 'bootswatch-rails'
gem 'ransack'
gem 'jquery-turbolinks'
gem 'kaminari'
gem 'bootstrap-kaminari-views'
gem 'jquery-ui-rails'
gem 'espinita'
gem 'mysqltopostgres', git: "https://github.com/maxlapshin/mysql2postgres.git"

group :development, :test do
  gem 'byebug'
  gem 'web-console', '~> 2.0'
  gem 'spring'
  gem 'rspec-rails'
  gem 'launchy'
  gem 'pry'
  gem 'pry-nav'
  gem 'shoulda-matchers'
  gem 'factory_girl_rails'
  gem 'capybara'
  gem 'newrelic_rpm'
  gem 'poltergeist'
  gem 'database_cleaner'
end

group :production do
  gem 'rails_12factor'
end
4
  • PG::UndefinedTable is an error from the PG (Postgres) gem. Did you update your Gemfile to use the mysql gem? See this part of the guide: devcenter.heroku.com/articles/cleardb#local-setup Commented Aug 2, 2015 at 10:09
  • Yes, I removed the pg gem from my gemfile, rebundled, pushed to heroku, and then ran heroku run rake db:create' but got this error: LoadError: cannot load such file -- pg. Then I tried to set the heroku database url to mysql2` and found this in my heroku logs: Cannot destroy last attachment to billing app for resource dozing-nimbly-2184 Commented Aug 2, 2015 at 23:02
  • Can you post your latest Gemfile? Commented Aug 2, 2015 at 23:04
  • @elithrar, here it is. I just added back the 'pg' gem though and it got rid of an error. See my comments in the proposed answer below. Commented Aug 3, 2015 at 6:49

1 Answer 1

3

This isn't an issue with ClearDB per se, it looks like you haven't fully divorced from the PostgreSQL defaults. I'd check:

  • is pg defined in your Gemfile? it shouldn't be. Instead ensure mysql2 is present.
  • Have you installed the Heroku ClearDB add-on for this app, and made it the default database your app will use? Have you uninstalled the Heroku Postgres database? See here for complete instructions.

Once your Heroku app can connect to the ClearDB database properly, you should be able to set up the database itself with no issues:

heroku run rake db:create
heroku run rake db:migrate
heroku run rake db:seed
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for posting, I hadn't deleted my Heroku Postgres database. I did so just now. I then set my new DB through heroku config:set and added a "2" after "mysql" since my Gemfile uses mysql2. But when I run heroku run rake db:create, I get the error: LoadError: cannot load such file -- pg. I'm confused because if I search for "pg" in my project, there are 0 results... So I'm not sure why a pg file is being loaded in the first place. I posted my Gemfile incase that helps.
Ok so based on some other internet info, I put 'pg' gem back into my Gemfile which got rid of that error. And it actually let me run heroku run rake db:create and heroku run rake db:seed successfully (I run that before my migrations, which I have to push after seeding). But then when I add the migration files and then run heroku rake db:migrate, I get this error: Mysql2::Error: Table 'heroku_74f570faead6d61.eligibility_states' doesn't exist: ALTER TABLE eligibility_states``. Do you know why my tables aren't being loaded in the seed file (where they are defined. Works in development.)?
If you do heroku pg:status I have a feeling you have created those in Postgres (given that you added the gem back and it "worked").
Hm, @BenPritchard I haven't run into that specific error but I strongly thing you shouldn't need the pg gem in your Gemfile. Perhaps you can look into the call stack for that error to try to figure out what file is trying to load pg? I'd be more comfortable getting that question answered, than I would with leaving an unnecessary gem in just because removing it causes an error; that feels like a recipe for future maintenance nightmares.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.