Skip to content
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

Disallow id as an enum value in Active Record #48527

Merged
merged 1 commit into from
Jun 20, 2023

Conversation

ghiculescu
Copy link
Member

Fixes #48524

The test case in the issue breaks because value.respond_to?(:id) returns true here. This effectively adds a default scope to queries where it shouldn't.

There might be a way to fix this in Active Record but I'd be surprised if nothing else breaks from defining id instance and class methods. I think it is simpler to not allow it as a value since it really should be treated as a reserved method.

You can use _prefix: true to work around this if you want to use id as a value.

Fixes rails#48524

The test case in the issue breaks because `value.respond_to?(:id)` returns true [here](https://github.com/rails/rails/blob/51f2e2f80b86e0c3769cf5272b7d97035730f19d/activerecord/lib/active_record/relation/predicate_builder.rb#L58). This effectively adds a default scope to queries where it shouldn't.

There might be a way to fix this in Active Record but I'd be surprised if nothing else breaks from defining `id` instance and class methods. I think it is simpler to not allow it as a value since it really should be treated as a reserved method.
@guilleiguaran guilleiguaran merged commit 8b36095 into rails:main Jun 20, 2023
8 of 9 checks passed
@ghiculescu ghiculescu deleted the active-record-enum-id branch June 20, 2023 02:50
skipkayhil added a commit to skipkayhil/rails that referenced this pull request Jun 20, 2023
…enum-id"

This reverts commit 8b36095, reversing
changes made to e05245d.

Railties tests have been failing since this change. The issue is that
calling `primary_key` as the model is loaded requires either a
connection to the database or a populated schema cache. This becomes an
issue when an app loads models that do not have underlying tables, as
shown in the failing Railties tests.

When eager loading an app using `rails/all`,
`ActionMailbox::InboundEmail` will be loaded whether or not `rails g
action_mailbox:install` has been run. This means the `primary_key` for
`InboundEmail` will not be in the schema cache and a database connection
will be required to boot the app.
guilleiguaran added a commit that referenced this pull request Jun 20, 2023
Revert "Merge pull request #48527 from ghiculescu/active-record-enum-id"
ghiculescu added a commit to ghiculescu/rails that referenced this pull request Jun 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Usage of id enum key results in unexpected filter with joins and subqueries
2 participants