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

ash_postgres.generate_migrations not working

It recently stopped generating migrations when there are attribute changes to resources. I get "No changes detected, so no migrations or snapshots have been created" I upgraded ash (3.5.24), ash_postgres (2.6.9) and ash_authentication (4.9.4), etc. but it didn't help. In fact, when I removed the "uuid_primary_key :id" line from Token and ran mix ash.codegen remove_id_from_tokens, it still insisted there is no change detected. ...
Solution:
so Token needs to be in some domain, probably Accounts, and Accounts needs to be mentioned in :ash_domains. ``` in config/config.exs import Config ...

AshAuthentication Testing

Need help setting up a function that mocks a logged in user in my conn. I'm using magic link authentication, this is what I've got so far: ```elixir...
Solution:
```elixir def log_in_user(conn, user) do strategy = AshAuthentication.Info.strategy!(MyApp.Accounts.User, :magic_link) {:ok, token} = AshAuthentication.Strategy.MagicLink.request_token_for(strategy, user) user = Accounts.sign_in_with_magic_link!(%{token: token}, authorize?: false)...

Unsupported expression in Elixir.AshPostgres.SqlImplementation query: `datetime_add`

Can't quite tell what I'm doing wrong here. The OrgSubscription is a many_to_many resource that connects Organization to SubscriptionPlan. ```elixir...
Solution:
You may need to use a fragment

No such input for atomic_update

Suppose I have a resource with attribute: ```elixir attributes do ......

Using no attributes to use a parent_type / parent_id style table as a join table

I have this table: ``` attributes do integer_primary_key :id ...
Solution:
Ok for anyone else coming across this post because they want to use no_attributes? true to do some complex mapping, here is the gist of it: - You can do a has_many or has_one to any App.Domain.Resource with some prereqs: - There must be a tangible path mapped up back to what you want - In my case I had...

How to model this runtime config in Ash?

What's the normal way to have a singleton resource that is saved in postgres, but has a read-only copy in ETS? I'm trying to think of how I might migrate one part of my app to Ash. In my app I have a global runtime configuration struct. Basically this is a config that admin users should be able to modify at runtime via a json API (or maybe the Ash admin UI 🤔) and it's supposed to affect the behavior of the system at runtime. Right now this config has a table in postgres with a custom unique index (true). This means that only one row is allowed in the table. The table itself just has one column which is a JSONB type. When the app starts up it reads this config and saves it with Application.put_env. Then in various places in the app I just fetch the config from the application env and pattern match out what I need. I could probably use persistent_term, but it's not a big struct. When a change is made to this config struct I update the database row and use Oban.Notifier to broadcast a message to all other instances of my app to tell them to refresh their application env cache of the config. What would be the Ash way to implement this? It doesn't have to work exactly like this. This is the best I came up with, but I'm happy to hear alternatives 🙂...

Test initialization takes more time than expected

I'm doing a green field project in Ash and it's been great so far. I'm feeling a bit lost when it comes to tests and their speed though. It takes about six seconds to run the test suite consisting of six tests, and that's enough for me to lose focus and start doing something else. I measure this by running time mix test. Elixir's test runner says it takes 0.5 seconds to run all tests, so something happens before the test runner starts. Right now my hypothesis is that `Getting extensions in cu...

"ERROR 42704 (undefined_object) type "vector" does not exist" during migration.

Hello, has someone tried the vectorization on a resource? I encounter a migration error problem. Current setup for vectorization of Post resource: 1. First, created setup_vectorization.ex in folder lib ...
Solution:
And then delete the migrations and snapshots you just generated

Is there any public function to prettify a BulkResult/Change error?

I want to display a :string error message but in this case there is none, i think AshJsonApi translates this errors to a message somehow, is that function public? ```elixir %Ash.BulkResult{ status: :partial_success,...
Solution:
so like AshJsonApi.Error.to_json_api_errors(Domain, Resource, error, :create)

ManualRead for wrapping an API using attribute constraints

I want to wrap an API with a resource. The response from the provider has a bunch of fields that can be "", which I'd prefer to be nil. Pretty sure this can be handled with constraints on attributes when the values are casted. The example in the docs shows manually creating the resource's struct from the API response and using Ash.Query.apply_to/2-3. Will this approach apply the constraints for the returned values doing this? Or is there a better way to make sure the constraints are applied?...
Solution:
I don't think it will cast those inputs, no

Is there a quick way to let Ash Authentication (Phoenix) inherit my custom daisyUI theme?

With Phoenix 1.18 (rc), daisy UI is now a thing. The thing I love about it is this theme builder, put into my css file, and tadah!, my app is fully customized. But ash_authentication ships with its own styles, which is fine on its own, but won't work well with phoenix from now on (imho). ...
Solution:
There is a PR up for this now 🙂

Possible regression on actions with upserts on custom identity and embedded unions

Hi, I have a resource action that used to work until recently (on Ash 3.4.58), but now errors out (on Ash 3.5.21 and 3.5.23). Could this be a regression or did I miss something about it in patch notes? I'm not exactly sure how to proceed because it seems to be expecting an identity it really shouldn't be....
Solution:
@czen can you try main of ash?

datetime_add shows error Could not cast function arguments for datetime_add/3

calculate :ends_at, :datetime, expr(datetime_add(starts_at, minutes, :minutes))
calculate :ends_at, :datetime, expr(datetime_add(starts_at, minutes, :minutes))
I couldn't find any examples of datetime_add and assumed it should work in expr. ...
Solution:
:minute instead of :minutes

How to conditionally do `run_oban_trigger`?

I am modifing code from the Ash.AI chat example. I could understand that whenever a message is created, it goes through one of the change change run_oban_trigger(:respond) which triggers the trigger :respond. So, whenever user type a message and enter, an LLM response is generated. I want to change it to only user explicitly type like "@M, xxxx", then the response from LLM should be triggered. How to do that?...
Solution:
```elixir @impl true def change(changeset, _opts, _context) do case Ash.Changeset.get_attribute(changeset, :text) do nil ->...

How to setup "AshPostgres.Extensions.Vector"

I want to use Ash.AI to build RAG feature for my app. Currently, I am not sure how to setup AshPostgres for vector. According to the document: https://hexdocs.pm/ash_postgres/AshPostgres.Extensions.Vector.html An extension that adds support for the vector type....
Solution:
It goes in the lib/ folder, and in a .ex file

opentelemetry_ash not ready for use or not properly configured?

Hi! We're struggling with some performance issue in prod and can see some gaps between the top level spans (typically, LiveView event handlers) and DB queries. So there's a suspicion that something might be going slow in building queries and/or processing results. So we've tried enabling opentelemetry with the opentelemetry_ash package (0.1.0). But it doesn't seem to hook correctly into the parent spans, looks kind of useless and hard to navigate. We use SigNoz to visualise the spans, it looks like on the screenshot....
No description

Creating a resource from another endpoint

Hey everyone! I’m running into a bit of a challenge and could use your input. I have an endpoint called locations, and I want to allow users to create leads as part of a POST request to: {{url}}/api/json/location...

Precondition checks and atomic

I often have to check the current value of a resource object in the DB as part of precondition checks of an action (e.g. when updating a "status" column I have to make sure it has a specific value before doing it). At first I tried to use "validation" for this but that seems to be only for input arguments and input attributes. So instead I used "change before_action" hooks to load the existing record and add an error to the Changeset if needed....

Is there a way to trigger a before_action without trying to run it?

I am doing some bulk-prevalidation where I am just doing an Ash.Changeset.for_create(..) to run validations. I need to load some related records based on a foregin key id in the inputs. Essentially I am validating if an architectural_element is valid, but it needs to use the study_id to load a study and all its related Outcrop so see that the chosen geology type exists in any of the related Outcrop. In a regular create I would just do a before action and look it up and then add error as needed. ...

Port conflict when running new app built using Ash installer

I used the Ash installer to generate a new app: ``` mix archive.install hex igniter_new --force mix archive.install hex phx_new 1.8.0-rc.3 --force...