I have the following count aggregate in my resource:
count :prior_year_transactions, :owner_records
in the same resource, the :owner_records
relationship is defined as such:
has_many :owner_records, Record do
no_attributes? true
read_action :valid
filter expr(
fragment("(? %> ?)", first_grantee_full_name, parent(first_owner[:full_name]))
)
end
It uses the :valid
read action from the Record
resource with is defined as such:
read :valid do
filter expr(
not is_nil(recording_date || instrument_date) and
(recording_date || instrument_date) >= ago(2, :year) and
not deleted?
)
prepare build(sort: [:recording_date], load: [:sell_record, :fix_and_flip?])
end
When I try to load that aggregate, I get the following error:
** (Ash.Error.Unknown) Unknown Error
* ** (Ecto.SubQueryError) the following exception happened when compiling a subquery.
** (Ecto.QueryError) deps/ash_postgres/lib/aggregate.ex:1049: field `property_id` in `select` does not exist in schema Pacman.Raw.Record in query:
from r0 in Pacman.Raw.Record,
as: 0,
where: fragment(
"(? %> ?)",
type(
as(0).first_grantee_full_name,
{:parameterized, Ash.Type.StringWrapper.EctoType, allow_empty?: false, trim?: true,
max_length: 255}
),
type(
fragment(
"jsonb_extract_path_text(?::jsonb,?::text)",
type(
parent_as(0).first_owner,
{:parameterized, Pacman.Markets.Property.Owner.EctoType, []}
),
^"full_name"
),
{:parameterized, Ash.Type.StringWrapper.EctoType, allow_empty?: false, trim?: true,
max_length: 255}
)
),
where: parent_as(0).id == as(0).property_id,
group_by: [r0.property_id],
select: %{
prior_year_transactions:
type(
coalesce(
count(type(as(0).id, {:parameterized, Ash.Type.UUID.EctoType, []})),
type(^0, {:parameterized, Ash.Type.Integer.EctoType, []})
),
{:parameterized, Ash.Type.Integer.EctoType, []}
),
property_id: map(r0, [:property_id]).property_id
}
The subquery originated from the following query:
from p0 in Pacman.Markets.Property,
as: 0,
left_lateral_join: r1 in subquery(from r0 in Pacman.Raw.Record,
as: 0,
where: fragment(
"(? %> ?)",
type(
as(0).first_grantee_full_name,
{:parameterized, Ash.Type.StringWrapper.EctoType, allow_empty?: false, trim?: true,
max_length: 255}
),
type(
fragment(
"jsonb_extract_path_text(?::jsonb,?::text)",
type(
parent_as(0).first_owner,
{:parameterized, Pacman.Markets.Property.Owner.EctoType, []}
),
^"full_name"
),
{:parameterized, Ash.Type.StringWrapper.EctoType, allow_empty?: false, trim?: true,
max_length: 255}
)
),
where: parent_as(0).id == as(0).property_id,
group_by: [r0.property_id],
select: merge(
%{
prior_year_transactions:
type(
coalesce(
count(type(as(0).id, {:parameterized, Ash.Type.UUID.EctoType, []})),
type(^0, {:parameterized, Ash.Type.Integer.EctoType, []})
),
{:parameterized, Ash.Type.Integer.EctoType, []}
)
},
map(r0, [:property_id])
)),
as: 1,
on: true,
where: type(
as(0).external_id,
{:parameterized, Ash.Type.StringWrapper.EctoType, allow_empty?: false, trim?: true,
max_length: 255}
) ==
type(
^"34025875",
{:parameterized, Ash.Type.StringWrapper.EctoType, allow_empty?: false, trim?: true,
max_length: 255}
),
where: type(as(0).external_id_type, {:parameterized, Ash.Type.Atom.EctoType, one_of: [:attom_id]}) ==
type(^"attom_id", {:parameterized, Ash.Type.Atom.EctoType, one_of: [:attom_id]}),
select: merge(
struct(p0, [
:id,
:external_id,
:external_id_type,
:cbsa_code,
:msa_code,
:minor_civil_division_code,
:full_address,
:house_number,
:street_direction,
:street_post_direction,
:street_name,
:street_suffix,
:unit_prefix,
:unit_value,
:city,
:county,
:county_fips,
:state,
:zip,
:zip_4,
:country,
:geography,
:year_built,
:lot_square_feet,
:lot_depth,
:lot_width,
:gross_area,
:living_area,
:garage_area,
:building_area,
:baths,
:partial_baths,
:bedrooms,
:rooms,
:units,
:stories,
:parking_spaces,
:zoning_code,
:type,
:roof_type,
:construction_type,
:air_conditioning_type,
:garage_types,
:basement_type,
:water_source,
:home_owners_association_type,
:multiple_listing_service_status,
:property_classification,
:last_sale,
:mortgage,
:assessment,
:tax,
:first_owner,
:second_owner,
:contact,
:acquisition_platform,
:publication_date,
:upload_date,
:attom_transaction_time,
:ap_transaction_time,
:deleted?,
:normalized_full_address,
:occupancy,
:inserted_at,
:updated_at
]),
%{
prior_year_transactions:
type(
coalesce(
type(as(1).prior_year_transactions, {:parameterized, Ash.Type.Integer.EctoType, []}),
type(^0, {:parameterized, Ash.Type.Integer.EctoType, []})
),
{:parameterized, Ash.Type.Integer.EctoType, []}
)
}
)
(ecto 3.10.3) lib/ecto/repo/queryable.ex:211: Ecto.Repo.Queryable.execute/4
(ecto 3.10.3) lib/ecto/repo/queryable.ex:19: Ecto.Repo.Queryable.all/3
(ash_postgres 1.3.60) lib/data_layer.ex:645: AshPostgres.DataLayer.run_query/2
(ash 2.17.1) lib/ash/actions/read.ex:2886: Ash.Actions.Read.run_query/6
(ash 2.17.1) lib/ash/actions/read.ex:1448: anonymous fn/4 in Ash.Actions.Read.data_field/3
(ash 2.17.1) lib/ash/engine/engine.ex:537: anonymous fn/2 in Ash.Engine.run_iteration/1
(ash 2.17.1) lib/ash/engine/engine.ex:558: anonymous fn/4 in Ash.Engine.async/2
(elixir 1.15.7) lib/task/supervised.ex:101: Task.Supervised.invoke_mfa/2
(elixir 1.15.7) lib/task/supervised.ex:36: Task.Supervised.reply/4
(ash 2.17.1) lib/ash/error/error.ex:524: Ash.Error.choose_error/2
(ash 2.17.1) lib/ash/error/error.ex:252: Ash.Error.to_error_class/2
(ash 2.17.1) lib/ash/actions/read.ex:211: Ash.Actions.Read.do_run/3
(ash 2.17.1) lib/ash/actions/read.ex:97: anonymous fn/3 in Ash.Actions.Read.run/3
(ash 2.17.1) lib/ash/actions/read.ex:96: Ash.Actions.Read.run/3
(ash 2.17.1) lib/ash/api/api.ex:2000: Ash.Api.read!/3
I’m not sure what I’m doing wrong of if this aggregate doesn’t work well with relationships that uses no_attributes?