# \#ash-query

**URL:** https://forum.elixirforum.com/tag/ash-query/3965.md

[Latest](https://forum.elixirforum.com/latest.md) · [Categories](https://forum.elixirforum.com/categories.md) · [Tags](https://forum.elixirforum.com/tags.md)

---

## [Using \`in\` or \`is\_nil()\` for a jsonb property](https://forum.elixirforum.com/t/using-in-or-is-nil-for-a-jsonb-property/72423)

<div class="topic-metadata">

**Author:** [@moxley](https://forum.elixirforum.com/u/moxley)\
**Replies:** 6\
**Last updated:** [September 14, 2025, 2:22am UTC](https://forum.elixirforum.com/t/using-in-or-is-nil-for-a-jsonb-property/72423 "2025-09-14T02:22:04Z")

</div>

I’m trying to understand the Ash filter expression syntax for working with json arrays within a jsonb column in ash\_postgres. I have a jsonb column, settings, on a table notification\_settings For a simple equality expr…

---

## [How do you filter data based on an union field?](https://forum.elixirforum.com/t/how-do-you-filter-data-based-on-an-union-field/72037)

<div class="topic-metadata">

**Author:** [@sezaru](https://forum.elixirforum.com/u/sezaru)\
**Replies:** 1\
**Last updated:** [August 12, 2025, 3:36pm UTC](https://forum.elixirforum.com/t/how-do-you-filter-data-based-on-an-union-field/72037 "2025-08-12T15:36:33Z")

</div>

I have this field in my resource: attribute :metadata, :union do public? true allow\_nil? false constraints types: \[ credit: \[type: Types.Credit, tag: :type, tag\_value: :credit\]…

---

## [How to reference the id of a struct argument to a read action](https://forum.elixirforum.com/t/how-to-reference-the-id-of-a-struct-argument-to-a-read-action/70012)

<div class="topic-metadata">

**Author:** [@MartinVolerich](https://forum.elixirforum.com/u/MartinVolerich)\
**Replies:** 10\
**Last updated:** [March 20, 2025, 3:33pm UTC](https://forum.elixirforum.com/t/how-to-reference-the-id-of-a-struct-argument-to-a-read-action/70012 "2025-03-20T15:33:52Z")

</div>

I’m stuck on how best to create a read action that takes a first argument of a struct (another Ash resource) that is used (via its id attribute) to filter the results of a query. My code is currently along the lines of: …

---

## [Aggregate the average story point for the week](https://forum.elixirforum.com/t/aggregate-the-average-story-point-for-the-week/69918)

<div class="topic-metadata">

**Author:** [@Sakthibalan](https://forum.elixirforum.com/u/Sakthibalan)\
**Replies:** 5\
**Last updated:** [March 13, 2025, 10:28am UTC](https://forum.elixirforum.com/t/aggregate-the-average-story-point-for-the-week/69918 "2025-03-13T10:28:21Z")

</div>

How to count weekly average points from tasks resource? defmodule Ash.Assignments.Milestone do ... attributes do uuid\_primary\_key :id attribute :title, :string attribute :description, :string attri…

---

## [Fetch unique users with distinct query](https://forum.elixirforum.com/t/fetch-unique-users-with-distinct-query/69915)

<div class="topic-metadata">

**Author:** [@Sakthibalan](https://forum.elixirforum.com/u/Sakthibalan)\
**Replies:** 1\
**Last updated:** [March 12, 2025, 1:47pm UTC](https://forum.elixirforum.com/t/fetch-unique-users-with-distinct-query/69915 "2025-03-12T13:47:18Z")

</div>

How to fetch unique user by ‘user\_id’ a query with distinct Resources: Ash.Domain.User Ash.Domain.Project has\_many :milestones Ash.Domain.Milestone belongs\_to :project has\_many :tasks Ash.Domain.Task bel…

---

## [\*\* (Ash.Error.Invalid) Invalid Error \* No such field name for resource](https://forum.elixirforum.com/t/ash-error-invalid-invalid-error-no-such-field-name-for-resource/69712)

<div class="topic-metadata">

**Author:** [@Chrichton](https://forum.elixirforum.com/u/Chrichton)\
**Replies:** 2\
**Last updated:** [March 1, 2025, 5:26pm UTC](https://forum.elixirforum.com/t/ash-error-invalid-invalid-error-no-such-field-name-for-resource/69712 "2025-03-01T17:26:12Z")

</div>

I am creating an admin app with data similar to the Tunez app from the Ash book from PragPro. I want to query the artists by name sorted by name. Using the tunez code, I am able to query via: Tunez.Music.search\_artis…

---

## [How To Chain Ash Preparations Without Defining them in an action?](https://forum.elixirforum.com/t/how-to-chain-ash-preparations-without-defining-them-in-an-action/69351)

<div class="topic-metadata">

**Author:** [@kamaroly](https://forum.elixirforum.com/u/kamaroly)\
**Replies:** 4\
**Last updated:** [February 11, 2025, 6:02pm UTC](https://forum.elixirforum.com/t/how-to-chain-ash-preparations-without-defining-them-in-an-action/69351 "2025-02-11T18:02:57Z")

</div>

Is there a way to chain preparations on a query directly without having to go through an action? I went through the online documentation, but I could not find how to do it. I am trying to do something that looks like t…

---

## [Failing to get aggregation to work with \`parent()\` and 3-layer nesting](https://forum.elixirforum.com/t/failing-to-get-aggregation-to-work-with-parent-and-3-layer-nesting/69244)

<div class="topic-metadata">

**Author:** [@moxley](https://forum.elixirforum.com/u/moxley)\
**Replies:** 16\
**Last updated:** [February 11, 2025, 3:47pm UTC](https://forum.elixirforum.com/t/failing-to-get-aggregation-to-work-with-parent-and-3-layer-nesting/69244 "2025-02-11T15:47:06Z")

</div>

I want to calculate the number of unread Posts for each User in a list of User IDs. Read (verb, past tense) Posts are tracked with PostView, by recording the ID of the last read Post for a given User ID. Here’s the SQL …

---

## [Ash.Query.after\_action/2 with relationships](https://forum.elixirforum.com/t/ash-query-after-action-2-with-relationships/68963)

<div class="topic-metadata">

**Author:** [@toberoni](https://forum.elixirforum.com/u/toberoni)\
**Replies:** 3\
**Last updated:** [January 24, 2025, 2:58pm UTC](https://forum.elixirforum.com/t/ash-query-after-action-2-with-relationships/68963 "2025-01-24T14:58:40Z")

</div>

I’m trying to understand how Ash.Query.after\_action/2 works and when relationships get loaded during the action lifecycle. The problem: In my read action I query some stories and a couple of relationships. I want to use…

---

## [The correct way to filter by an interval in Ash](https://forum.elixirforum.com/t/the-correct-way-to-filter-by-an-interval-in-ash/60237)

<div class="topic-metadata">

**Author:** [@dewetblomerus](https://forum.elixirforum.com/u/dewetblomerus)\
**Replies:** 3\
**Last updated:** [December 8, 2024, 7:42pm UTC](https://forum.elixirforum.com/t/the-correct-way-to-filter-by-an-interval-in-ash/60237 "2024-12-08T19:42:49Z")

</div>

I am trying to filter records by a calculated interval. My resource has these two attributes: attribute :retry\_at, :utc\_datetime, allow\_nil?: true attribute :tried\_at, :utc\_datetime, allow\_nil?: true I tried t…

---

## [Using Ash.Query.add\_error()](https://forum.elixirforum.com/t/using-ash-query-add-error/67948)

<div class="topic-metadata">

**Author:** [@moxley](https://forum.elixirforum.com/u/moxley)\
**Replies:** 7\
**Last updated:** [December 8, 2024, 6:53am UTC](https://forum.elixirforum.com/t/using-ash-query-add-error/67948 "2024-12-08T06:53:16Z")

</div>

I’m creating a read AshGraphql query, and I want it to return an error for certain conditions. When I call Ash.Query.add\_error(query, "some error message") or similar in the action prepare, the GraphQL query returns: %{…

---

## [Compare nullable fields in filters](https://forum.elixirforum.com/t/compare-nullable-fields-in-filters/66336)

<div class="topic-metadata">

**Author:** [@sezaru](https://forum.elixirforum.com/u/sezaru)\
**Replies:** 3\
**Last updated:** [September 26, 2024, 7:59pm UTC](https://forum.elixirforum.com/t/compare-nullable-fields-in-filters/66336 "2024-09-26T19:59:22Z")

</div>

I have this filter in my action: |\> Ash.Query.filter(organization\_id == ^actor.organization\_id) The issue with it is that organization\_id can be null, so when that happen, I will get this warning: warning: Comparing v…

---

## [Dynamically building an AND expression with Ash.Query](https://forum.elixirforum.com/t/dynamically-building-an-and-expression-with-ash-query/64990)

<div class="topic-metadata">

**Author:** [@moxley](https://forum.elixirforum.com/u/moxley)\
**Replies:** 5\
**Last updated:** [July 18, 2024, 1:42pm UTC](https://forum.elixirforum.com/t/dynamically-building-an-and-expression-with-ash-query/64990 "2024-07-18T13:42:32Z")

</div>

I’m trying to query members that have all the given tag IDs. The code needs to dynamically build an expression like: member.member\_tags.tag\_id = tag\_id1 AND member.member\_tags.tag\_id = tag\_id2 AND member.member\_tags.tag…

---

## [Read action that does a contains search](https://forum.elixirforum.com/t/read-action-that-does-a-contains-search/64933)

<div class="topic-metadata">

**Author:** [@superchris](https://forum.elixirforum.com/u/superchris)\
**Replies:** 5\
**Last updated:** [July 16, 2024, 12:30am UTC](https://forum.elixirforum.com/t/read-action-that-does-a-contains-search/64933 "2024-07-16T00:30:16Z")

</div>

I’m sure this must be a trivial thing to do, but in looking through the documentation so far I have not figured this out. I’m trying to make a simple read action that does a substring search and the syntax is just not cl…

---

## [All places that uses filter should have the same behavior](https://forum.elixirforum.com/t/all-places-that-uses-filter-should-have-the-same-behavior/64478)

<div class="topic-metadata">

**Author:** [@sezaru](https://forum.elixirforum.com/u/sezaru)\
**Replies:** 4\
**Last updated:** [June 25, 2024, 2:00pm UTC](https://forum.elixirforum.com/t/all-places-that-uses-filter-should-have-the-same-behavior/64478 "2024-06-25T14:00:28Z")

</div>

I’m upgrading one of my projects from Ash 2.0 to 3.0 and one of the things that changed was this: resource.actions.read.filter can now be specified multiple times. Multiple filters will be combined with and . I do li…

---

## [Build Ash.Query dynamically from other Ash.Query structs](https://forum.elixirforum.com/t/build-ash-query-dynamically-from-other-ash-query-structs/63324)

<div class="topic-metadata">

**Author:** [@moxley](https://forum.elixirforum.com/u/moxley)\
**Replies:** 2\
**Last updated:** [May 5, 2024, 11:09am UTC](https://forum.elixirforum.com/t/build-ash-query-dynamically-from-other-ash-query-structs/63324 "2024-05-05T11:09:45Z")

</div>

I’m building a module that builds up an Ash.Query from parameters. I want it to be able to “OR” two queries together, like this: left\_query = build\_query(Member2, left\_params) right\_query = build\_query(Member2, right\_pa…

---

## [Correct way to filter with an inner join using Ash.Query](https://forum.elixirforum.com/t/correct-way-to-filter-with-an-inner-join-using-ash-query/62553)

<div class="topic-metadata">

**Author:** [@rbino](https://forum.elixirforum.com/u/rbino)\
**Replies:** 5\
**Last updated:** [March 27, 2024, 7:26pm UTC](https://forum.elixirforum.com/t/correct-way-to-filter-with-an-inner-join-using-ash-query/62553 "2024-03-27T19:26:13Z")

</div>

In my application I want to retrieve all tags that are currently assigned to at least a device. In Ecto I did this with: def list\_device\_tags do query = from t in Tag, distinct: true, join: …

---

## [Use Ash keyset pagination with Ecto queries?](https://forum.elixirforum.com/t/use-ash-keyset-pagination-with-ecto-queries/61690)

<div class="topic-metadata">

**Author:** [@sezaru](https://forum.elixirforum.com/u/sezaru)\
**Replies:** 14\
**Last updated:** [February 20, 2024, 6:40pm UTC](https://forum.elixirforum.com/t/use-ash-keyset-pagination-with-ecto-queries/61690 "2024-02-20T18:40:49Z")

</div>

I have an Ash query that I need to convert into an Ecto query to do some more complex manipulations (group by, etc) using the Ash.Query.data\_layer\_query/2 function. Is there any way for me to either transform my Ecto qu…

---

## [Ash.Query.set\_argument don't work inside an action](https://forum.elixirforum.com/t/ash-query-set-argument-dont-work-inside-an-action/60650)

<div class="topic-metadata">

**Author:** [@sezaru](https://forum.elixirforum.com/u/sezaru)\
**Replies:** 2\
**Last updated:** [December 30, 2023, 10:44pm UTC](https://forum.elixirforum.com/t/ash-query-set-argument-dont-work-inside-an-action/60650 "2023-12-30T22:44:09Z")

</div>

I have a read action that I want to do some checks first. To do that, I want to create some preparations, the first one will fetch some data, and the others will use that data to do some checks. Here is the preparation…

---

## [No attribute left join relantionship not giving correct results](https://forum.elixirforum.com/t/no-attribute-left-join-relantionship-not-giving-correct-results/59750)

<div class="topic-metadata">

**Author:** [@sezaru](https://forum.elixirforum.com/u/sezaru)\
**Replies:** 15\
**Last updated:** [November 15, 2023, 11:47pm UTC](https://forum.elixirforum.com/t/no-attribute-left-join-relantionship-not-giving-correct-results/59750 "2023-11-15T23:47:51Z")

</div>

Hi, I want to translate the following query to a query inside Ash with my resource: select buy\_record.transaction\_id as buy\_transaction, sell\_record.transaction\_id as sell\_transaction, buy\_record.external\_…

---

## [How to run a second query inside a read action?](https://forum.elixirforum.com/t/how-to-run-a-second-query-inside-a-read-action/59746)

<div class="topic-metadata">

**Author:** [@sezaru](https://forum.elixirforum.com/u/sezaru)\
**Replies:** 2\
**Last updated:** [November 15, 2023, 8:30pm UTC](https://forum.elixirforum.com/t/how-to-run-a-second-query-inside-a-read-action/59746 "2023-11-15T20:30:43Z")

</div>

Hey, I have a read query that uses pg\_trgm and I want to set word\_similarity\_threshold before running it. I tried something like this: read :by\_entity do argument :entity\_full\_name, :string, allow\_nil?: false…
