Ash FrameworkAF
Ash Framework2mo ago
11 replies
Anonymous

Tagged union type for an attribute on a table

Hello.
I have a context field in my task table which would be a different JSON on the basis of task_type so I decided to use a union type to model this attribute, in the following way.

defmodule Electra.Mobile.Task.Context do
  use Ash.Type.NewType,
    subtype_of: :union,
    constraints: [
      types: [
        dt_indexing: [
          type: Electra.Mobile.Task.DtIndexing,
          tag: :task_type,
          tag_value: :dt_indexing,
          cast_tag?: false
        ]
      ]
    ]
end


This is how I am creating my context during the task creation -

    %Ash.Union{
      type: :dt_indexing,
      value: %{
        asset_id: to_string(asset.id),
        dt_name: dt_name,
        dt_code: dt_code,
        dt_capacity: dt_capacity,
        dt_location: dt_location,
        feeder_code: feeder_code,
        feeder_name: feeder_name
      }
    }


However, when I try to create the task, it logs the query for the task_creation and then silently rolls back. I only see this error which is not really clear to me. It says that it's not able to load the context type because it doesn't match any types but I am absolutely matching the structure.


What could be causing this? Any sort of guidance would be really helpful. Thank you.
Was this page helpful?