rbino

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?

Showing Posts 1 to 5

BryanJBryce

BryanJBryce

I wonder if using exists here might help: Ash.Query.Exists — ash v3.29.3

Something maybe like this? (sorry, not in a place where I can try this out myself)

Tag
|> Ash.Query.filter(exists(:device_tags))
|> Ash.Query.set_tenant(tenant_id)
|> MyApi.read!()
rbino

rbino OP

Ash Core Team

This fails in the same way the first two fail:

** (Ash.Error.Unknown) Unknown Error

* filter: Invalid reference device_tags when hydrating relationship ref for [:device_tags]. Require single attribute primary key.

If I pass another field from the relationships

Tag
|> Ash.Query.filter(exists(:device_tags.device_id))
|> MyApi.read!(tenant: tenant)

it fails with

** (Ash.Error.Invalid) Input Invalid

* got KeyError with message "key :resource not found in: %Ash.Error.Query.NoSuchFunction{\n  name: :exists,\n  arity: nil,\n  changeset: nil,\n  query: nil,\n  error_context: [],\n  vars: [],\n  path: [:filter],\n  stacktrace: #Stacktrace<>,\n  class: :invalid\n}" while retrieving Exception.message/1 for %Ash.Error.Query.NoSuchFunction{name: :exists, arity: nil, changeset: nil, query: nil, error_context: [], vars: [], path: [:filter], stacktrace: #Stacktrace<>, class: :invalid}.

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

rbino OP

Ash Core Team

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

User
|> Ash.Query.filter(not is_nil(posts))
|> Api.read!()

to obtain the users that have at least one post (without having to do, e.g., posts.title like I have to do in my application).

Still, this is doing an outer join, not an inner join:

[debug] QUERY OK source="tags" db=1.5ms queue=0.5ms idle=1346.0ms
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 (NOT (d1."tag_id"::bigint IS NULL)) AND (t0."tenant_id"::bigint = $1::bigint) [1] module=Ecto.Adapters.SQL function=log/4
zachdaniel

zachdaniel

Creator of Ash
Tag
|> Ash.Query.filter(exists(device_tags, true))
|> MyApi.read!(tenant: tenant)

Something like the above should do it :slight_smile:

rbino

rbino OP

Ash Core Team

Thanks, this works perfectly!

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews