diff --git a/lib/ecto/adapters/sqlite3/connection.ex b/lib/ecto/adapters/sqlite3/connection.ex index 50c4d04..669d0a3 100644 --- a/lib/ecto/adapters/sqlite3/connection.ex +++ b/lib/ecto/adapters/sqlite3/connection.ex @@ -1340,6 +1340,10 @@ defmodule Ecto.Adapters.SQLite3.Connection do ["json_extract(", expr(expr, sources, query), ", '$", path, "')"] end + def expr({:exists, _, [subquery]}, sources, query) do + ["exists", expr(subquery, sources, query)] + end + def expr({fun, _, args}, sources, query) when is_atom(fun) and is_list(args) do {modifier, args} = case args do diff --git a/test/ecto/integration/crud_test.exs b/test/ecto/integration/crud_test.exs index 1279470..ef8d036 100644 --- a/test/ecto/integration/crud_test.exs +++ b/test/ecto/integration/crud_test.exs @@ -233,5 +233,16 @@ defmodule Ecto.Integration.CrudTest do assert [_] = TestRepo.all(from(a in Account, where: a.email == "hi@hi.com")) assert [_] = TestRepo.all(from(a in Account, where: a.email == "HI@HI.COM")) end + + test "handles exists subquery" do + account1 = TestRepo.insert!(%Account{name: "Main"}) + user1 = TestRepo.insert!(%User{name: "John"}, []) + TestRepo.insert!(%AccountUser{user_id: user1.id, account_id: account1.id}) + + subquery = + from(au in AccountUser, where: au.user_id == parent_as(:user).id, select: 1) + + assert [_] = TestRepo.all(from(a in Account, as: :user, where: exists(subquery))) + end end end