Ash FrameworkAF
Ash Framework7mo ago
69 replies
Jesse Williams

Potential bug with relationship `parent` references

Bit of a gnarly one ahead... 😅

I'm running onto what I think is a bug with parent in relationship filters. My setup looks something like this:

1. A resource called GuideLevel with a has_many relationship with no_attributes? true and a filter. This relationship is called ancestors and uses an AshPostgres.Ltree column to get the ancestors of the current record, although idt the ltree behavior is relevant to the bug. The filter expression looks like: filter expr(fragment("? @> ? AND ? < ?", level, parent(level), nlevel, parent(nlevel))). Note that this references an nlevel calculation which just calculates the nlevel of the ltree column: calculate :nlevel, :integer, expr(fragment("nlevel(?)", level))
2. A few calculations on GuideLevel that use a calculation module, which implements a sort of "inheritance" structure for a given setting on the GuideLevel. It looks like this:
def expression(opts) do
  source_field = Keyword.fetch!(opts, :source_field)
  default_value = Keyword.fetch!(opts, :default_value)
  expr(
    if is_nil(^ref(source_field)) do
      # closest ancestor with a non-nil source_field
      first(ancestors,
        query: [
          sort: [nlevel: :desc],
          filter: expr(not is_nil(^ref(source_field)))
        ],
        field: ^source_field
      ) || ^default_value
    else
      ^ref(source_field)
    end
  )
end

where source_field is the source to pull the given setting from.

(continued in thread)
Solution
@Jesse Williams thanks for the tests, I've got a fix
Was this page helpful?