Skip to content

Commit 7e10ac7

Browse files
newmanjeffJeff Newman
and
Jeff Newman
authored
Fixes exists expressions (#92)
Co-authored-by: Jeff Newman <[email protected]>
1 parent e3e29ed commit 7e10ac7

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

lib/ecto/adapters/sqlite3/connection.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,6 +1340,10 @@ defmodule Ecto.Adapters.SQLite3.Connection do
13401340
["json_extract(", expr(expr, sources, query), ", '$", path, "')"]
13411341
end
13421342

1343+
def expr({:exists, _, [subquery]}, sources, query) do
1344+
["exists", expr(subquery, sources, query)]
1345+
end
1346+
13431347
def expr({fun, _, args}, sources, query) when is_atom(fun) and is_list(args) do
13441348
{modifier, args} =
13451349
case args do

test/ecto/integration/crud_test.exs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,5 +233,16 @@ defmodule Ecto.Integration.CrudTest do
233233
assert [_] = TestRepo.all(from(a in Account, where: a.email == "[email protected]"))
234234
assert [_] = TestRepo.all(from(a in Account, where: a.email == "[email protected]"))
235235
end
236+
237+
test "handles exists subquery" do
238+
account1 = TestRepo.insert!(%Account{name: "Main"})
239+
user1 = TestRepo.insert!(%User{name: "John"}, [])
240+
TestRepo.insert!(%AccountUser{user_id: user1.id, account_id: account1.id})
241+
242+
subquery =
243+
from(au in AccountUser, where: au.user_id == parent_as(:user).id, select: 1)
244+
245+
assert [_] = TestRepo.all(from(a in Account, as: :user, where: exists(subquery)))
246+
end
236247
end
237248
end

0 commit comments

Comments
 (0)