Rails raises Undeclared attribute type for enum after an upgrade to 7.1 or later
Your error
Undeclared attribute type for enum 'screen_location' in Banner. Enums must be backed by a database column or declared with an explicit type via `attribute`.1 more line it can showFix it fast
Most common cause, 1 of 3The column exists in a migration that has not run in this environment
Run the pending migrations for that environment.
bin/rails db:migrate
bin/rails db:migrate RAILS_ENV=test
# or rebuild the test database after edited migrations
bin/rails test:dbShort answer
Rails 7.1 and later raise Undeclared attribute type for enum when no database column backs the enum and no attribute call gives it a type; Rails 7.0 let this pass. Run pending migrations in that environment, match the enum name to the real column, or add attribute :name, :string above a virtual enum. Applies to Rails 7.1 to 8.1.
Which cause do you have?
Ask the database which columns the table has in the environment that fails, without loading the model itself. If the enum name is missing from the list, compare it with the migration status (cause 1) and with similar column names (cause 2). If the enum was never meant to be a column, go to cause 3.
bin/rails runner 'ActiveRecord::Base.with_connection { |c| p c.columns("banners").map(&:name) }'
bin/rails db:migrate:status | tail -n 5Then find the line you see in this list and open its fix:
- 1The column exists in a migration that has not run in this environment
db:migrate:status shows the migration that adds the column as down, often only in test or on one server - 2The enum name does not match the column name
The column list shows something close, such as location, screen_location_id or a renamed column, but not the exact enum name - 3The enum sits on a virtual attribute with no declared type
The model has no column of that name by design: the value is computed, set by a form or kept only in memory
What does the error look like?
Undeclared attribute type for enum 'screen_location' in Banner. Enums must be backed by a database column or declared with an explicit type via `attribute`.The error appears only after bumping Rails from 7.0 to 7.1 or later; the same model loaded fine before
Cause 1 of 3
The column exists in a migration that has not run in this environment
You seedb:migrate:status shows the migration that adds the column as down, often only in test or on one server
Check
Rails builds the enum's type from the column it finds in the connected database. When the migration that adds the column has not run there, the lookup finds nothing and the check added in 7.1 stops the model. The test database is a common place for this, since it only gets new schema when it is prepared.
RAILS_ENV=test bin/rails db:migrate:status | grep downFix
Run the pending migrations for that environment. For the test database, migrate it or rebuild it from the schema file. In production, make sure the deploy runs migrations before the new code starts serving.
bin/rails db:migrate
bin/rails db:migrate RAILS_ENV=test
# or rebuild the test database after edited migrations
bin/rails test:dbVerify
The column list now includes the enum's name and the model loads.
bin/rails runner 'p Banner.screen_locations'Confirmed by guides.rubyonrails.org
Cause 2 of 3
The enum name does not match the column name
You seeThe column list shows something close, such as location, screen_location_id or a renamed column, but not the exact enum name
Check
The first argument to enum must be the attribute name. A typo, a column renamed or removed in a later migration, or a foreign-key style _id column leaves the enum pointing at nothing. Before 7.1 such an enum loaded without complaint, so the mistake can be years old and only show up during the upgrade.
grep -n "screen_location" db/schema.rb app/models/banner.rbFix
Point the enum at the real column, or rename the column with a migration. If the column was dropped on purpose, delete the enum and its callers.
class Banner < ApplicationRecord
# column in db/schema.rb is "location", not "screen_location"
enum :location, { top: 0, sidebar: 1, footer: 2 }
endVerify
The model loads in the console and Banner.locations returns the mapping.
bin/rails runner 'p Banner.locations'Confirmed by raw.githubusercontent.com
Cause 3 of 3
The enum sits on a virtual attribute with no declared type
You seeThe model has no column of that name by design: the value is computed, set by a form or kept only in memory
Check
Since Rails 7.1, an enum on an attribute that has no column must be preceded by an attribute declaration with an explicit type. Without it, Rails only sees the default value type, which the enum code rejects with this message.
grep -n "attribute :\|enum " app/models/banner.rbFix
Declare the attribute with the type its values use, then define the enum below it. Use the positional enum form, because Rails 8.0 removed the old enum status: {...} keyword style.
class Banner < ApplicationRecord
attribute :screen_location, :string
enum :screen_location, { top: "top", sidebar: "sidebar" }
endVerify
Banner.new(screen_location: :top).top? returns true without the error.
bin/rails runner 'p Banner.new(screen_location: :top).top?'Confirmed by raw.githubusercontent.com
How do I stop it coming back?
- Run bin/rails db:migrate:status in the deploy script and stop the release when any line shows down.
- Keep a model test that loads every model with an enum, so a missing column fails in CI rather than on the first production request.
- When a migration removes or renames a column, search app/models for enum lines that use the old name in the same pull request.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.