Reverse of type in ecto select

Hi there

I’ve recently stumbled upon the question if there is a “reverse” for the type function in Ecto. I.e. if you write a query like this:

id = Ecto.UUID.generate()

query = from u in "user", where u.id == type(^id, Ecto.UUID)

you need to tell Ecto what type the value of id is using the type above.

Now I sort of want to reverse this using a complex select (using Postgres), something like this, so that the result map will already have converted the entity_ids to string representations of the UUID’s:

query = from ap in AccountPermission, 
  where: ...., 
  group_by: ap.permission_name, 
  select: %{
    ap.permission_name,  
    entities: fragment("array_agg(?)", untype(ap.entity_id, Ecto.UUID))
  }

Where I’ve made up the part about the untype function.

Qustion: Is there such a mechanism? Because I’ve been searching through the docs and the source code, but was not able find one.

Thanks

Hi
The same type function should be used:

entities: type(fragment("array_agg(?)", ap.entity_id), {:array, Ecto.UUID})
1 Like

Thanks. Thought so, but just couldn’t figure out where to put it :slight_smile:

1 Like