mirror of
https://github.com/ash-project/ash_postgres.git
synced 2024-09-20 05:23:18 +12:00
improvement: upgrade to new exists
usage
This commit is contained in:
parent
e8643d4aa0
commit
6ad57501ff
4 changed files with 54 additions and 6 deletions
|
@ -732,12 +732,12 @@ defmodule AshPostgres.Expr do
|
|||
|
||||
defp do_dynamic_expr(
|
||||
query,
|
||||
%Exists{path: [first | rest], expr: expr},
|
||||
%Exists{at_path: at_path, path: [first | rest], expr: expr},
|
||||
bindings,
|
||||
_embedded?,
|
||||
_type
|
||||
) do
|
||||
resource = query.__ash_bindings__.resource
|
||||
resource = Ash.Resource.Info.related(query.__ash_bindings__.resource, at_path)
|
||||
first_relationship = Ash.Resource.Info.relationship(resource, first)
|
||||
|
||||
filter = %Ash.Filter{expression: expr, resource: first_relationship.destination}
|
||||
|
@ -760,7 +760,7 @@ defmodule AshPostgres.Expr do
|
|||
ref_binding(
|
||||
%Ref{
|
||||
attribute: Ash.Resource.Info.attribute(resource, first_relationship.source_attribute),
|
||||
relationship_path: [],
|
||||
relationship_path: at_path,
|
||||
resource: resource
|
||||
},
|
||||
bindings
|
||||
|
@ -788,8 +788,6 @@ defmodule AshPostgres.Expr do
|
|||
field(through, ^first_relationship.source_attribute_on_join_resource),
|
||||
select: 1
|
||||
)
|
||||
|
||||
# )
|
||||
else
|
||||
Ecto.Query.from(destination in filtered,
|
||||
select: [1],
|
||||
|
|
3
mix.exs
3
mix.exs
|
@ -138,7 +138,8 @@ defmodule AshPostgres.MixProject do
|
|||
{:ecto, "~> 3.8"},
|
||||
{:jason, "~> 1.0"},
|
||||
{:postgrex, ">= 0.0.0"},
|
||||
{:ash, ash_version("~> 2.0.0-rc.9")},
|
||||
{:ash,
|
||||
ash_version(github: "ash-project/ash", ref: "3b803d4c552f0df3f8d07a8da9bb1bdd4dfc8fc6")},
|
||||
{:git_ops, "~> 2.4.5", only: :dev},
|
||||
{:ex_doc, "~> 0.22", only: :dev, runtime: false},
|
||||
{:ex_check, "~> 0.14", only: :dev},
|
||||
|
|
|
@ -622,6 +622,54 @@ defmodule AshPostgres.FilterTest do
|
|||
|> Api.read!()
|
||||
end
|
||||
|
||||
test "it works with an `at_path`" do
|
||||
post =
|
||||
Post
|
||||
|> Ash.Changeset.new(%{title: "a"})
|
||||
|> Api.create!()
|
||||
|
||||
other_post =
|
||||
Post
|
||||
|> Ash.Changeset.new(%{title: "other_a"})
|
||||
|> Api.create!()
|
||||
|
||||
Comment
|
||||
|> Ash.Changeset.new(%{title: "comment"})
|
||||
|> Ash.Changeset.manage_relationship(:post, post, type: :append_and_remove)
|
||||
|> Api.create!()
|
||||
|
||||
Comment
|
||||
|> Ash.Changeset.new(%{title: "comment"})
|
||||
|> Ash.Changeset.manage_relationship(:post, other_post, type: :append_and_remove)
|
||||
|> Api.create!()
|
||||
|
||||
Post
|
||||
|> Ash.Changeset.new(%{title: "b"})
|
||||
|> Ash.Changeset.manage_relationship(:linked_posts, [post], type: :append_and_remove)
|
||||
|> Api.create!()
|
||||
|
||||
Post
|
||||
|> Ash.Changeset.new(%{title: "b"})
|
||||
|> Ash.Changeset.manage_relationship(:linked_posts, [other_post], type: :append_and_remove)
|
||||
|> Api.create!()
|
||||
|
||||
assert [%{title: "b"}] =
|
||||
Post
|
||||
|> Ash.Query.filter(
|
||||
linked_posts.title == "a" and
|
||||
linked_posts.exists(comments, title == ^"comment")
|
||||
)
|
||||
|> Api.read!()
|
||||
|
||||
assert [%{title: "b"}] =
|
||||
Post
|
||||
|> Ash.Query.filter(
|
||||
linked_posts.title == "a" and
|
||||
linked_posts.exists(comments, title == ^"comment")
|
||||
)
|
||||
|> Api.read!()
|
||||
end
|
||||
|
||||
test "it works with nested relationships inside of exists" do
|
||||
post =
|
||||
Post
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
defmodule AshPostgres.Test.Money do
|
||||
@moduledoc false
|
||||
use Ash.Resource,
|
||||
data_layer: :embedded
|
||||
|
||||
|
|
Loading…
Reference in a new issue