Skip to content

Explicitly declaring attributes #833

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Israel-Molestina
Copy link

@Israel-Molestina Israel-Molestina commented Jul 9, 2025

At RailsConf2025 I had the pleasure of pairing with Leon Vogt.
While setting up the app, I ran into this error:
Undeclared attribute type for enum 'kind' in Talk. Enums must be backed by a database column or declared with an explicit type via attribute.
when seeding the data.

From this stack overflow and the linked github issue, it seems that when you define an enum without explicitly specifying the attribute type, Rails expects the underlying database column to be an integer.

For a fix I explicitly declared these attributes as strings.

@Israel-Molestina Israel-Molestina changed the title Updating kind attributes Explicitly declaring attributes Jul 9, 2025
@Israel-Molestina Israel-Molestina marked this pull request as ready for review July 9, 2025 20:00
@adrienpoly
Copy link
Member

I am surprise about this error, I have been using string enum quite a lot in this project and others and never had this issue. Not saying they are no issue, just that it feels like it could be hiding something else.

In our case we have a db column. So I don't even understand the error message.

I wonder if our migration are somehow corrupted? We don't see that unless we start fresh and run a full db:migrate?

@Israel-Molestina
Copy link
Author

@adrienpoly I'm unable to reproduce this error, but another attendee at RailsConf also encountered this same issue when he first cloned the repo and ran the seed file, and @leonvogt also encountered this issue, but they were able to get around it by dropping the database.

I also do agree with you that this might be hiding an underlying issue. Here seems to be the original PR in Rails 7.1 that adds support to non-column-backed attributes for enum, but your attributes are backed by columns, so again not sure where the error is coming from.

Since I'm unable to reproduce this error unfortunately I can't give a straight answer as to what is happening under the hood, and from my time spent googling the problem it seems its not a one off occurrence and has been happening since Rails 7.1.

It does seem that explicitly declaring the attribute is just a work around for a deeper problem.

Heres another issue I found That implies it might be an issue with the way enums are initialized.

@marcoroth
Copy link
Member

Yeah, somehow this error happened on at least 5 attendees machines. It's quite bizarre.

@leonvogt
Copy link
Contributor

leonvogt commented Jul 11, 2025

Yeah, that's a very strange issue. I don't really understand what's going on here.

I experienced the same error a few days ago after pulling the latest commits from main and running db:migrate.
I proceeded to drop and recreate the database, thinking there might just be a mismatch with my existing local DB.

That fixed the issue for me.
However, other attendees at RailsConf, who never had a local DB and freshly cloned the repo, experienced the same error. So it doesn't seem to be related to whether you already have a DB or not.

I tried to reproduce the error, but so far I haven’t had any success.

@zhephyn
Copy link
Contributor

zhephyn commented Jul 11, 2025

I ran into this same error when setting up my database locally yesterday and I'd like to provide some more context:

First and foremost, I was setting up the database for the very first time on my machine. I think potential reproduction steps may include:

  1. Dropping the existing database(s)
  2. Deleting the folder/copy of the repository on your local machine
  3. Cloning the repository again and thereafter trying to setup your database again.

The error(s) will be seen when you try to run bin/rails db:migrate

Secondly, this PR is meant to fix 3 errors not just one errors. 2 of these errors are enum errors while the other error is a NoMethodError.

First enum error:

== 20241019135118 CreateTalkFts: migrating ====================================
-- create_virtual_table(:talks_search_index, :fts5, ["title", "summary", "speaker_names", "tokenize = porter"])
   -> 0.0070s
bin/rails aborted!
StandardError: An error has occurred, this and all later migrations canceled: (StandardError)

Undeclared attribute type for enum 'kind' in Talk. Enums must be backed by a database column or declared with an explicit type via `attribute`.
/home/zhephyn/Desktop/rubyevents/app/models/talk/searchable.rb:27:in 'Talk::Searchable::ClassMethods#reindex_all'
/home/zhephyn/Desktop/rubyevents/db/migrate/20241019135118_create_talk_fts.rb:8:in 'CreateTalkFts#up'

Caused by:
Undeclared attribute type for enum 'kind' in Talk. Enums must be backed by a database column or declared with an explicit type via `attribute`.
/home/zhephyn/Desktop/rubyevents/app/models/talk/searchable.rb:27:in 'Talk::Searchable::ClassMethods#reindex_all'
/home/zhephyn/Desktop/rubyevents/db/migrate/20241019135118_create_talk_fts.rb:8:in 'CreateTalkFts#up'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

Which is solved by adding this line above the enum declarations in the talk.rb model file:

  attribute :kind, :string

Second enum error:

== 20250614131810 AddKindToEvent: migrating ===================================
-- add_column(:events, :kind, :string, {default: "event", null: false})
   -> 0.0076s
-- add_index(:events, :kind)
   -> 0.0035s
bin/rails aborted!
StandardError: An error has occurred, this and all later migrations canceled: (StandardError)

Undeclared attribute type for enum 'date_precision' in Event. Enums must be backed by a database column or declared with an explicit type via `attribute`.
/home/zhephyn/Desktop/rubyevents/db/migrate/20250614131810_add_kind_to_event.rb:6:in 'Enumerator#each'
/home/zhephyn/Desktop/rubyevents/db/migrate/20250614131810_add_kind_to_event.rb:6:in 'AddKindToEvent#up'

Caused by:
Undeclared attribute type for enum 'date_precision' in Event. Enums must be backed by a database column or declared with an explicit type via `attribute`.
/home/zhephyn/Desktop/rubyevents/db/migrate/20250614131810_add_kind_to_event.rb:6:in 'Enumerator#each'
/home/zhephyn/Desktop/rubyevents/db/migrate/20250614131810_add_kind_to_event.rb:6:in 'AddKindToEvent#up'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

Which I had solved by adding this line above the enum declarations in the event.rb model file:

  attribute :date_precision, :string

Then finally the NoMethodError:

== 20250128070756 AddUniqueIndexOnGitHubInSpeaker: migrating ==================
bin/rails aborted!
StandardError: An error has occurred, this and all later migrations canceled: (StandardError)

undefined method 'assign_canonical_speaker!' for nil
/home/zhephyn/Desktop/rubyevents/db/migrate/20250128070756_add_unique_index_on_github_in_speaker.rb:14:in 'AddUniqueIndexOnGitHubInSpeaker#up'

Caused by:
NoMethodError: undefined method 'assign_canonical_speaker!' for nil (NoMethodError)

    Speaker.find_by(name: "Andrew").assign_canonical_speaker!(canonical_speaker: Speaker.find_by(name: "Andrew Nesbitt"))
                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^
/home/zhephyn/Desktop/rubyevents/db/migrate/20250128070756_add_unique_index_on_github_in_speaker.rb:14:in 'AddUniqueIndexOnGitHubInSpeaker#up'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
zhephyn@mail:~/Desktop/rubyevents$ 

Which I had solved by making each of the lines nil safe:

    Speaker.find_by(name: "Andrew")&.assign_canonical_speaker!(canonical_speaker: Speaker.find_by(name: "Andrew Nesbitt"))

@marcoroth marcoroth force-pushed the main branch 2 times, most recently from d35f39a to ec30df0 Compare August 9, 2025 18:51
@adrienpoly adrienpoly mentioned this pull request Aug 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants