Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"ActionView::Template::Error (Unknown primary key for table" after trying to push local database to Heroku

For a Ruby-on-Rails app that I'm hosting through Heroku, I recently downloaded a backup, restored it locally and then added data to the database from outside sources. That all worked fine.

After the updated database was pushed to Heroku using heroku pg:push <localdbname> HEROKU_DATABASE_URL --app <appname> the app was working fine and I could see the data that was newly added.

However today when I tried to log in to the app on Heroku, I was getting error messages. In my log file I saw this error:

ActionView::Template::Error (Unknown primary key for table ...

According to this SO post: Getting "Unknown primary key for table" while the ID is there

one user was able to get around this problem by resetting and pushing the database to Heroku several times. However, that hasn't worked for me. I've tried to reset and push the database at least 4 times now.

One possibility is that my local postgres database is using version 9.6 and the app is using version 9.4. The Heroku database is a Hobby-Basic database. There is documentation from Heroku on how to upgrade the Postgres version but it's not totally clear from their example what I would need to do. I'm guessing that I basically need to create a new database, copy the data from the old to the new and then destroy the old. Has anyone done this before? Is there a fee associated with doing so? And has upgrading the version of Postgres remotely fixed this issue for other people?

like image 865
themantalope Avatar asked Aug 27 '17 21:08

themantalope


2 Answers

I have ran into similar issues before. I was creating a postgres database outside of rails in RazorSQL. I imported data from outside sources and when I ran it locally everything worked fine. When it came to deployment I ran into all sorts of issues.

  1. I created tables outside of rails thus no migrations were created. I had to recreate the tables by deleting the current model and generating a new model which is a copy of the old one only this time a migration is created for example:

    rails generate model ad name:string description:text price:decimal seller_id:integer email:string img_url:string

  2. I needed to import data from the database I had already created and before I did anything I actually created a seed file using this gem https://github.com/rroblak/seed_dump. All I had to do when I created the model again (remember to delete the model and recreate it) was run rake db:seedand it pulled the data in.

3.The last thing I had to do was insert the assocations in the models for example in a owner model putting in has many: customersto connect the customer model.

Another scenario was I ran a query in RazorSQL and generated a new table from the query and imported it into postgres. The problem was it was only a table from results thus it did not have a primary key. I had to manually create that in postgres using ALTER TABLE test1 ADD COLUMN id SERIAL PRIMARY KEY; This gave me an auto incrementing primary key field and I have to give credit to this answer which helped me https://stackoverflow.com/a/2944561/7039895.

Hope this helps.

like image 184
Jermaine Subia Avatar answered Oct 15 '22 03:10

Jermaine Subia


According to the docs, you several choices of how to upgrade your database. pg:copy and pg:upgrade.

In your case, I recommand pg:copy: this is easier but required your database to be "off" while your doing the upgrade. This should not be an issue since your database is not currently working.

pg:upgrade should only be used when the downtime required for a PG copy upgrade is unacceptably long for your business.

Upgrade with PG copy: (All the steps are explain in depth in the link)

  • Provision new database

You need to create a new database, it will be automatically using the last version of postgresql for heroku (in your case 9.6)

heroku addons:create heroku-postgresql:standard-0
  • Prevent new database updates

Stop you current database from writing mode to avoid corrupted data while copying to the new

heroku maintenance:on
  • Transfer data to new database

You need to copy all the data from the old DB to the new.

heroku pg:copy DATABASE_URL HEROKU_POSTGRESQL_PINK --app sushi
  • Promote new database

You need to notify heroku that you will use the new DB and not the old one.

heroku pg:promote HEROKU_POSTGRESQL_PINK
  • Last step: Make application active

Everything should be good by now, just make your DB active so it can save new query.

heroku maintenance:off

As you can see, all the steps are straightforward. Therefore, if upgrading you version of postgreSQL doesn't solve the issue, you can still switch back to the old one before removing it.

Pricing

I think it depends of your subscription already (Hobby, Standard or Premium) https://www.heroku.com/pricing. It may be free, or cheap, but I think it also depends of the size of your DB.

To be sure it won't cost you too much, I'm sure you can ask directly to Heroku support https://devcenter.heroku.com/articles/paid-support.

like image 22
Kruupös Avatar answered Oct 15 '22 04:10

Kruupös