-
Notifications
You must be signed in to change notification settings - Fork 116
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
base: main
Are you sure you want to change the base?
Explicitly declaring attributes #833
Conversation
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? |
@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. |
Yeah, somehow this error happened on at least 5 attendees machines. It's quite bizarre. |
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. That fixed the issue for me. I tried to reproduce the error, but so far I haven’t had any success. |
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:
The error(s) will be seen when you try to run Secondly, this PR is meant to fix 3 errors not just one errors. 2 of these errors are First == 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 attribute :kind, :string Second == 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 attribute :date_precision, :string Then finally the == 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")) |
d35f39a
to
ec30df0
Compare
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.