-
Notifications
You must be signed in to change notification settings - Fork 43
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
Lack of FROM subquery name can break aggregate functions #35
Comments
To make matters more complex, you cannot qualify the column if the relation is already loaded: users = User.foos.union(User.bars)
users.load
# raises ActiveRecord::UnmodifiableRelation:
users.maximum('users.updated_at')
# raises ActiveRecord::StatementInvalid:
users.maximum(:updated_at)
# workaround:
users.collect(&:updated_at).max I'll open up a PR soon. |
This also effects the relation's users = User.foos.union(User.bars).joins(:account)
# raises ActiveRecord::StatementInvalid:
users.ids First test passes, second fails: def test_union_ids_without_join
account = Account.create!
user_1 = User.create!(account:, role: 'foo')
user_2 = User.create!(account:, role: 'bar')
user_3 = User.create!(account:, role: 'bar')
union = User.foos.union(
User.bars,
)
assert_not_raised ActiveRecord::StatementInvalid do
assert_equal union.ids, [user_1.id, user_2.id, user_3.id]
end
end
def test_union_ids_with_join
account = Account.create!
user_1 = User.create!(account:, role: 'foo')
user_2 = User.create!(account:, role: 'bar')
user_3 = User.create!(account:, role: 'bar')
union = User.foos.union(
User.bars,
)
assert_not_raised ActiveRecord::StatementInvalid do
assert_equal union.joins(:account).ids, [user_1.id, user_2.id, user_3.id]
end
end Workaround: union.pluck(:"#{union.table_name}.id") |
ezekg
added a commit
to keygen-sh/keygen-api
that referenced
this issue
Nov 22, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Not sure if this is a bug in Rails or this gem, but since a subquery name is not provided to
from
here (even though it's technically available on theArel::Nodes::TableAlias
instance), Active Record can't determine its table name. This results in AR using an unqualified column name, which can cause ambiguous column errors for aggregate functions likemaximum(:updated_at)
if there's a join after the union, resulting in anActiveRecord::StatementInvalid
error getting raised.Ideally, AR should be a bit smarter here, since right now it tries to cast the
Arel::Nodes::TableAlias
instance to a string. I'd assume they'd argue Arel is a private API and that this isn't a bug, so I opened the issue here first.Here's a quick script reproducing the
ActiveRecord::StatementInvalid
error:Unfortunately, in addition to one-off aggregations, this also breaks
stale?
andfresh_when
caching/etag controller methods, since they usemaximum(:updated_at)
to calculate the cache's last modified timestamp.The only workaround I could come up with is explicitly qualifying the column name:
Do you think this is worth fixing?
The text was updated successfully, but these errors were encountered: