Merge pull request #40387 from jhawthorn/exists_on_contradiction
Avoid query from exists? on contradictory relation
This commit is contained in:
commit
64b1c81532
@ -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?
|
||||
|
Loading…
Reference in New Issue
Block a user