rbino
Ash Core Team
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: dt in DeviceTag,
on: t.id == dt.tag_id
Repo.all(query)
end
What is the correct way to do the same thing with Ash.Query?
Additional info
DeviceTag is the join table between Device and Tag, and it has a composite primary key of tenant_id + device_id + tag_id (I’m using attribute-based multitenancy).
In the Ash version I’ve also added a has_many :device_tags relationship on the Tag (which was needed before) to use it in filters.
What I tried
Tag
|> Ash.Query.filter(device_tags)
|> Ash.read!(tenant: tenant)
Results in:
** (Ash.Error.Unknown) Unknown Error
* filter: Invalid reference device_tags when hydrating relationship ref for [:device_tags]. Require single attribute primary key.
Tag
|> Ash.Query.filter(length(device_tags) > 1)
|> Ash.read!(tenant: tenant)
Results in the same error as above
Tag
|> Ash.Query.filter(device_tags.device_id)
|> Ash.read!(tenant: tenant)
Results in:
[debug] QUERY ERROR source="tags" db=0.0ms queue=17.2ms idle=1659.4ms
SELECT DISTINCT ON (t0."id") t0."id", t0."name", t0."inserted_at", t0."updated_at", t0."tenant_id" FROM "tags" AS t0 LEFT OUTER JOIN "public"."devices_tags" AS d1 ON t0."id" = d1."tag_id" WHERE (d1."device_id"::bigint) AND (t0."tenant_id"::bigint = $1::bigint) [1] module=Ecto.Adapters.SQL function=log/4
** (Ash.Error.Unknown) Unknown Error
* ** (Postgrex.Error) ERROR 42804 (datatype_mismatch) argument of AND must be type boolean, not type bigint
Tag
|> Ash.Query.filter(not is_nil(device_tags.device_id))
|> Ash.read!(tenant: 1)
Seems to do what I want, but the check on device_id seems redundant.
Is this actually the way to go or am I missing something?
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #security










Showing Posts 1 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
BryanJBryce
I wonder if using
existshere might help: Ash.Query.Exists — ash v3.29.3Something maybe like this? (sorry, not in a place where I can try this out myself)
rbino
This fails in the same way the first two fail:
If I pass another field from the relationships
it fails with
Since I get a lot of errors on the composite foreign key, I’m trying out in a fresh repo with “normal” primary keys to see if that’s the problem.
rbino
I’ve tried playing around in the Ash test suite and the only thing I gain from not having a composite foreign key is that I can do
to obtain the users that have at least one post (without having to do, e.g.,
posts.titlelike I have to do in my application).Still, this is doing an outer join, not an inner join:
zachdaniel
Something like the above should do it
rbino
Thanks, this works perfectly!