Merge pull request #40387 from jhawthorn/exists_on_contradiction

Avoid query from exists? on contradictory relation
This commit is contained in:
John Hawthorn 2020-10-15 11:06:54 -07:00 committed by GitHub
commit 64b1c81532
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 0 deletions

@ -315,6 +315,7 @@ def exists?(conditions = :none)
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

@ -444,6 +444,18 @@ def test_marshal_load_legacy_relation
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?