How do I get `fragment` in scope in a `prepare` block?
I'm wanting to sort a query using a fragment. I found this https://elixirforum.com/t/how-do-i-sort-records-by-random-in-the-resource-action/58774 that looks like it would work. However, I get an error when I try.
My code:
The error I get
error: undefined function fragment/2 (expected MyModule to define such a function or for it to be imported, but none are available)
.
How do I use fragment
in the prepare function? Or is there a better way to do this?7 Replies
I'm already requiring Ash.Query btw.
fragment is part of AshPostgres IIRC do you use AshPostgres as the datalayer on the resource?
maybe AshSqlite as well
Yes. Thanks. I'll look there.
ok, I'm wrong the builtins support fragment too
Adding
Ecto.Query.API.fragment
at least got it to compile. I can't find how to fully qualify it in Ash.
Nevermind, that didn't work either.
Btw, trying prepare build(sort: [desc: fragment("1 - (embedding <=> ?)", arg(:embedding))])
gets the same error.try
Ash.Query.sort(query, Ash.Sort.expr_sort(expr(fragment("1 - (embedding <=> ?)", arg(:embedding)))))
I think you might need to wrap it in expr(..)
I got it! This works:
https://hexdocs.pm/ash/Ash.Query.html#sort/3-expression-sorts
Thanks for your help.