Rails error after upgrading to Postgres 10


After upgrading my pre-5.0 Rails app to Postgres 10.1, I see errors such as:

ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR:  column "increment_by" does not exist
LINE 1: ...gets_id_seq"', (SELECT COALESCE(MAX("id")+(SELECT increment_...
:               SELECT setval('"public"."widgets_id_seq"', (SELECT COALESCE(MAX("id")+(SELECT increment_by FROM "public"."widgets_id_seq"), (SELECT min_value FROM "public"."widgets_id_seq")) FROM "widgets"), false)


This is caused by a change in the way Postgres implements ID sequences. The column increment_by was changed to Increment, and Rails isn't aware of this so it tries to pull the old column, leading to an error.

There are a few solutions to this. I'll list them in preference order:

Upgrade to the latest Rails

Or to at least Rails 5.0.x. This is the oldest version of Rails that had the patch for this backported. Upgrading is relatively straightforward from Rails 4.2.x. You can see the full changes to upgrade to Rails 5.1.4 on the Ruby Getting Started app. It consists mostly of changes to migrations (specify a version of the class to inherit from. 4.2 is the safest version) and tests (wrap parameters in controller tests in params: { ... } and remove assigns references).

Use an initializer to monkey patch in the fix

You can find a piece of code that worked for someone on the GitHub issue for this problem in Rails.

Upgrade to Postgres 9.6 instead of Postgres 10.

If you can't upgrade Rails or patch in the fix, you can upgrade Postgres to 9.6 instead of 10 by specifying the version when running pg:upgrade: pg:upgrade --version 9.6. This commit shows how to specify in your app.json that your CI and Review Apps and application forks should use Postgres 9.6.

Ask on Stack Overflow

Engage with a community of passionate experts to get the answers you need

Ask on Stack Overflow

Heroku Support

Create a support ticket and our support experts will get back to you

Contact Heroku Support