Ash Framework

AF

Ash Framework

The Elixir backend framework for unparalleled productivity. Declarative tools that let you stop wasting time. Use with Phoenix LiveView or build APIs in minutes for your front-end of choice.

Join

support

showcase

today-i-learned

indeterminate_datatype with fragment using the PosGIS

i need determine if a record its inside a polygon that its a param from the call, but returns ERROR 42P18 (indeterminate_datatype) could not determine data type of parameter $1 and dont really know why its happening, i tried on direct sql query and works well with the format and returns the polygon, so dont know why fails with on the format i think that maybe its injecting like POLYGON(("latitude longitude,latitude longitude") and maybe quotes its making some conflict but dont really know, same query with same data but in direct sql its working ```elixir...

How do I use a schema macro in an extension?

I want to make an Ash extension that adds a :uuid_v7_primary_key attribute to a resource. Adding an attribute via a macro: This is an antipattern but it’s a small example....
Solution:
YOu can do Ash.Resource.Builder.add_new_attribute(dsl_state, ....)

Advice for policy simplicity

Hey, I'm just getting into policies and wanted some advice on how to make this easy to work with. So I want all code interface actions to permit calling any function from the application itself or IEx. But I want all external callers to be verified for access. Is this a good start? ```elixir ...
Solution:
This option: define :foo, default_options: [authorize?: false]

Ash AI vector_cosine_distance

When attempting to use vector_cosine_distance as shown here https://github.com/ash-project/ash_ai?tab=readme-ov-file#using-the-vectors, the compiler can't find the function. I also can't find anything with that name in the ash_ai repo. How does this work?

How to call actions on an embedded resource with code interface?

I've started working on my custom authentication in my app and the first step is to make a resource representing a logged in user. This resource is embedded cause I plan to only use it as an actor. I won't save it anywhere for now. So as usual, I made some actions and a code interface for them, but I can't call them from IEx. So how do I call them? ...
Solution:
```elixir code_interface do domain YOur.Domain define :create define :from_token...

Igniter crash

I'm running into an issue on Windows where Igniter crashes with the following error during ash_authentication setup, getting an arguement error {:error, :enoent}. It seems to be coming from a call to System.cmd("tty", []) in the tty?/0 function, which fails because tty doesn’t exist on Windows....

How to compose read actions?

I have a read action with a small prepare that selects a few attributes and filters. I want to make a new read action that uses the same query as the previous read action, but also loads a calculation and a few relationships. How do I "compose" this with the old read action?...
Solution:
You don't compose them together, you share a preparation between the two actions

Custom error strings

Some of the error messages that show up on form validation read a bit awkwardly in my opinion (such as "is required"), so I would like to provide my own. Is there any way to do this other than intercepting them in the CoreComponents.translate_error/1 function in the Phoenix 1.7 project template?
Solution:
You can use the transform_errors option: https://hexdocs.pm/ash_phoenix/AshPhoenix.Form.html...

Sorting error: NoSuchField

Getting this error when trying to sort documents that have been uploaded by users:
(MatchError) no match of right hand side value: {:error, %Ash.Error.Invalid{errors: [%Ash.Error.Query.NoSuchField{resource: MyApp.Ash.Accounts.UserUpload, field: "uploaded_file_name", splode: Ash.Error, bread_crumbs: [], vars: [], path: [:sort], stacktrace: #Splode.Stacktrace<>, class: :invalid}]}}
(MatchError) no match of right hand side value: {:error, %Ash.Error.Invalid{errors: [%Ash.Error.Query.NoSuchField{resource: MyApp.Ash.Accounts.UserUpload, field: "uploaded_file_name", splode: Ash.Error, bread_crumbs: [], vars: [], path: [:sort], stacktrace: #Splode.Stacktrace<>, class: :invalid}]}}
Function that results in this error:...

Calculation returning array of strings

I have a calculation that returns an array of strings. ```elixir calculations do calculate :namevariations, {:array, :string}, fn , _ -> ["hello", "world"]...
Solution:
```elixir calculations do calculate :namevariations, {:array, :string}, fn records, -> Enum.map(records, fn _ -> ["hello", "world"] end) end...

Restricting aggregate calculations

I have an "aircraft" resource that has this aggregate: ```elixir sum :total_flight_hours_mins, :tech_logs, :airborne_time_mins do description "Total flight hours in minutes for this aircraft" end...
Solution:
```elixir calculate ..., expr(sum(tech_logs, field: :airborne_time_mins, query: [ filter: [expr(... ^arg(:foobar))] ])) do argument :foobar, ......

Pagination of primary action breaks showing relationships

I think I may have found a bug. I have resources A and B with a many_to_many relationship. I edited the primary read action of B to make it require pagination with required?: true. This is because I wanted to make it so that even if someone calls my API without a limit, the default limit per page is still applied. But now, when I go to A in the Admin UI, click on a row, then click the button to load its related Bs, nothing happens. No SQL query is executed. When I un-require the pagination, then it works. Yet, when I click the second button that only shows the IDs of the rows of the "join_through" resource, that one works....

Code interface vs domain resource block

I’ve read the docs a few times and I can’t tell whether code interfaces are recommended in Ash 3. I seem to remember that they are a vestige of previous versions but I don’t see any mention that they are deprecated or discouraged. Is there something that code interfaces can do that I can’t do in a domain resource block?
Solution:
I don't really use it as well. I mostly just think about them as functions

AshAuthentication custom strategy UI

I am trying to implement a custom strategy that verifies the user login with some custom rules, but after following this documentation i am stuck on where to implement the UI (https://hexdocs.pm/ash_authentication/custom-strategy.html) Is there any demo project that can serve as a reference for this? Do i just use UI Overrides? (https://hexdocs.pm/ash_authentication_phoenix/ui-overrides.html)...

n8n cannot access mcp client in Phoenix

Im running an MCP server in my Phoenix application. MCP Inspector says that everything is set up correctly. Now Im trying to access that MCP server locally from n8n using the MCP client node but it keeps saying that it could not connect. Phoenix returns the following error: (Phoenix.Router.NoRouteError) no route found for POST /%7B%22url%22:%22http:/localhost:4000/mcp%22%7D n8n seems to send a json object in the url In the MCP Client Node in the SSE Endpoint field I have provided http://localhost:4000/mcp ...

Can't disable derive_filter? in json API

I have this json API definition in my domain ```elixir json_api do routes do base_route "/runtime-config", MyApp.RuntimeConfig do...

Action-specific parameter sanitization

Is there a way to sanitize a random map of params that is used for a specific action? Here's my exact use case: - I have a map coming from query string params - That map may contain extra or invalid keys...
Solution:
When calling the action you can do skip_unknown_inputs: :*

AshJsonApi with InertiaJs

Is there a way to leverage AshJsonApi without... the api part? By that I mean the serializtion, public? attributes, etc.

code_interface, compile time vs runtime

if this were a code_interface, is timestamp captured once at compile time and all future calls use that compiled timestamp value? If yes, is there a different way to solve this? I've got multiple live views calls this code_interface and I'm wanting to keep all the logic in one place ```elixir define :get_budget_with_accounts, action: :read,...
Solution:
i.e default_options: fn -> ... end

Multitenancy with code interface on the domain?

So I have this: ```elixir defmodule MyApp.FlyingTrainingOrg do use Ash.Domain # ......
Solution:
There is a tenant option to all code interfaces 🙂