Ash FrameworkAF
Ash Framework3mo ago
11 replies
Jesse Williams

unselected attribute used in relationship filter + aggregate results in column does not exist error

I'm not sure at what level of the stack this bug is happening, but I suspect it's at the ash_sql level.

I have an ltree based resource. I decided to use relationships/calculations to abstract some of the ltree details away, so I have a children relationship that looks like this (level is the ltree column, and nlevel is a calculation that is just nlevel(level)):

has_many :children, MyResource do
  public? true
  no_attributes? true

  # use ltree <@ operator to get descendants, where nlevel is exactly 1 more
  # than the parent's nlevel (that is, direct children)
  filter expr(
           fragment(
             """
             ? <@ ? AND ? = ? + 1
             """,
             level,
             parent(level),
             nlevel,
             parent(nlevel)
           )
         )
end


I then have this aggregate:

count :count_children,
      :children do
  public? true
end


Using ash_graphql, I grab some resources like this (note I do not select level):

query GetResources($ids: [ID!]) {
  myResources(filter: {id: {in: $ids}}) {
    id
    countChildren
  }
}


This results in the following error from postgres:

** (Postgrex.Error) ERROR 42703 (undefined_column) column s0.level does not exist

It seems like at some point, ash loses track of the fact it needs to include the level field in the SQL select so that it can correlate the relationship to it, even though it doesn't need to be included in the gql response.
Solution
There is an open bug about this in ash_postgres
Was this page helpful?