Heroku rake db:migrate does not create tables (Rails 5)
Asked Answered
A

1

1

I have a Rails 5 app and I wanted to destroy and rebuild my live database (site not launched yet). So I followed the steps that should've worked (they used to work in the past):

  1. heroku pg:reset HEROKU_POSTGRESQL_HEROKUCOLOR_URL --confirm {app_name}
  2. heroku run rake db:migrate
  3. heroku run rake db:seed

Steps 1 & 2 complete successfully and step 3 fails with the error ('adminsettings' is one of my tables):

Running rake db:seed on {app_name}... starting, run.7198 (Hobby)
Running rake db:seed on {app_name}... connecting, run.7198 (Hobby)
Running rake db:seed on {app_name}... up, run.7198 (Hobby)
rake aborted!
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  relation "adminsettings" does not exist
LINE 8:                WHERE a.attrelid = '"adminsettings"'::regclas...

If I run heroku pg:info it returns I have 2 tables (I should have about 10) so it is as if the step 2 failed (even though there was no error). If I check pgAdmin4 I can only see 2 tables: ar_internal_metadata and schema_migrations which are not my custom tables.

I deleted all migrations after ensuring they are in my schema.rb file so there are actually no migrations necessary - just the table creations. So my first question is: Does (should!) the 'heroku rake db:migrate' just use the schema.rb file to build the tables?

This is a Rails 5.2.2 app but it started off as Rails 4.x app and I upgraded all gems along the way. Would this be a cause somehow? There was no issue up to now except when I tried this destructive rebuild. Otherwise, I'm out of ideas. And I don't have much debug visibility into what is going wrong.

Ambala answered 25/9, 2019 at 20:24 Comment(0)
D
2

rake db:migrate won't load the schema.rb file. It'll only run the migrations. If you want to load the schema then you can use rake db:schema:load.

You might also need to be careful if you reinstate your migrations to run again as the schema_migrations table probably has a record of all your migrations having run already. You'd need to clear that table too (delete the records - not drop the table) in order to indicate that your migrations should run again.

Destine answered 26/9, 2019 at 7:59 Comment(1)
Perfect advice! So running 'heroku run rake db:schema:load' created the tables correctly and then 'heroku run rake db:seed' subsequently worked fine. So my steps should have been: - 1. heroku pg:reset HEROKU_POSTGRESQL_HEROKUCOLOR_URL --confirm {app_name} 2. heroku run rake db:schema:load 3. heroku run rake db:migrate 4. heroku run rake db:seedAmbala

© 2022 - 2024 — McMap. All rights reserved.