Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions lib/join.ex
Original file line number Diff line number Diff line change
Expand Up @@ -407,9 +407,7 @@ defmodule AshSql.Join do
filter_subquery? = Keyword.get(opts, :filter_subquery?, false)
parent_resources = Keyword.get(opts, :parent_stack, [relationship.source])

read_action =
relationship.read_action ||
Ash.Resource.Info.primary_action!(relationship.destination, :read).name
read_action = get_read_action(relationship)

context = Map.delete(query.__ash_bindings__.context, :data_layer)

Expand All @@ -436,17 +434,17 @@ defmodule AshSql.Join do
end)
|> Ash.Query.do_filter(opts[:apply_filter], parent_stack: parent_resources)
|> then(fn query ->
if query.__validated_for_action__ == read_action do
if query.__validated_for_action__ == read_action.name do
query
else
Ash.Query.for_read(query, read_action, %{},
Ash.Query.for_read(query, read_action.name, %{},
actor: context[:private][:actor],
tenant: context[:private][:tenant]
)
end
end)
|> Ash.Query.unset([:distinct, :select, :limit, :offset])
|> handle_attribute_multitenancy(tenant)
|> handle_attribute_multitenancy(tenant, read_action)
|> hydrate_refs(context[:private][:actor])
|> then(fn query ->
if sort? do
Expand Down Expand Up @@ -507,8 +505,9 @@ defmodule AshSql.Join do
end

@doc false
def handle_attribute_multitenancy(query, tenant) do
if tenant && Ash.Resource.Info.multitenancy_strategy(query.resource) == :attribute do
def handle_attribute_multitenancy(query, tenant, read_action \\ nil) do
if tenant && Ash.Resource.Info.multitenancy_strategy(query.resource) == :attribute &&
(is_nil(read_action) || read_action.multitenancy not in [:bypass, :bypass_all]) do
multitenancy_attribute = Ash.Resource.Info.multitenancy_attribute(query.resource)

if multitenancy_attribute do
Expand Down Expand Up @@ -1367,4 +1366,12 @@ defmodule AshSql.Join do
{dynamic, acc} = AshSql.Expr.dynamic_expr(root_query, filter, bindings, true)
{from(row in query, where: ^dynamic), acc}
end

defp get_read_action(%{read_action: nil} = rel) do
Ash.Resource.Info.primary_action!(rel.destination, :read)
end

defp get_read_action(rel) do
Ash.Resource.Info.action(rel.destination, rel.read_action)
end
end
6 changes: 5 additions & 1 deletion lib/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -644,14 +644,18 @@ defmodule AshSql.Query do
[first_rel_name | _] ->
relationship = Ash.Resource.Info.relationship(resource, first_rel_name)

if is_nil(relationship) do
raise "No such relationship #{inspect(resource)}.#{relationship}. aggregates: #{inspect(aggregates)}"
end

rel_fields =
if !Map.get(relationship, :no_attributes?) && Map.get(relationship, :source_attribute) do
[relationship.source_attribute]
else
[]
end

if relationship && relationship.filter do
if relationship.filter do
extract_parent_attrs_from_filter(relationship.filter, query)
else
[]
Expand Down