1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Avoid query from exists? on contradictory relation

Co-authored-by: Adam Hess <HParker@github.com>
This commit is contained in:
John Hawthorn 2020-10-14 12:30:10 -07:00
parent c9357cd3e3
commit 3b622bc0d6
2 changed files with 13 additions and 0 deletions

View file

@ -315,6 +315,7 @@ module ActiveRecord
end
relation = construct_relation_for_exists(conditions)
return false if relation.where_clause.contradiction?
skip_query_cache_if_necessary { connection.select_rows(relation.arel, "#{name} Exists?").size == 1 }
end

View file

@ -444,6 +444,18 @@ module ActiveRecord
end
end
test "no queries on empty relation exists?" do
assert_queries(0) do
Post.where(id: []).exists?(123)
end
end
test "no queries on empty condition exists?" do
assert_queries(0) do
Post.all.exists?(id: [])
end
end
private
def skip_if_sqlite3_version_includes_quoting_bug
if sqlite3_version_includes_quoting_bug?