From b3c25c8bfa4832581ccef9d7dda185d3756f8568 Mon Sep 17 00:00:00 2001 From: David Biddle Date: Fri, 4 Apr 2025 10:41:33 +0100 Subject: [PATCH] Prevent ActiveRecord errors when adding columns Rails uses [prepared statements](https://www.postgresql.org/docs/current/sql-prepare.html) to query the database. By default these use wildcard select statements. This commonly causes issues when adding new columns - the new column will cause postgres to throw an error `cached plan must not change result type`. In Rails 7, a new config setting, `enumerate_columns_in_select_statements`, was added. This replaces the wildcard select statements with explicit calls to each column. See [the Rails PR](rails/rails#41718) for details This means that the prepared statements will be regenerated every time a column is added, so postgres won't throw the error. This PR turns that config setting on for our ActiveRecord models. --- config/application.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/config/application.rb b/config/application.rb index 50f4ee6e..ff37d787 100644 --- a/config/application.rb +++ b/config/application.rb @@ -67,5 +67,8 @@ class Application < Rails::Application h[:trace_id] = event.payload[:trace_id] if event.payload[:trace_id] end end + + # Prevent ActiveRecord::PreparedStatementCacheExpired errors when adding columns + config.active_record.enumerate_columns_in_select_statements = true end end