pascal1

pascal1

How to create a relationship which filters by comparing the value of a nested resource of the target resource against the value of the source resource?

I’m trying to create a relationship which filters by comparing the value of a nested resource of the target resource against the value of the source resource.

Source Resource:

defmodule App.Alerts.AlertRule do

  attributes do
    uuid_primary_key :id

    attribute :key, :string do
      allow_nil? false
      public? true
      default "Env"
    end

    attribute :value, :string do
      allow_nil? false
      public? true
      default "Prod"
    end
end

Target Resource

defmodule App.Deployments.Deployment do

  relationships do
    has_many :deployment_tags, App.Deployments.DeploymentTag do
      destination_attribute :deployment_id
    end
  end
end

defmodule App.Deployments.DeploymentTag do
  attributes do
    uuid_primary_key :id

    attribute :key, :string do
      allow_nil? false
      constraints max_length: 20
      public? true
    end

    attribute :value, :string do
      allow_nil? false
      constraints max_length: 20
      public? true
    end

  end

end

Inside the source resource I’ve the following relationship defined:

    has_many :monitored_deployments, App.Deployments.Deployment do
      no_attributes? true
      filter expr(workspace_id == parent(workspace_id))
      filter expr(exists(deployment_tags, key == parent(key) and value == parent(value)))
    end

The first filter expression with the workspace_id seems to work fine.
In the second filter the key and value fields of the parent (which I expect to point to the AlertRule) can’t be referenced.

 ** (Ash.Error.Unknown)
     Bread Crumbs:
       > Error returned from: App.Alerts.AlertRule.read

     Unknown Error

     * Invalid reference key
         at monitored_deployments, filter
       (ash 3.5.26) ....../deps/splode/lib/splode.ex:322: Ash.Error.to_error/2
       (ash 3.5.26) lib/ash/query/query.ex:4359: Ash.Query.add_error/3
       (ash 3.5.26) lib/ash/actions/read/relationships.ex:329: Ash.Actions.Read.Relationships.related_query/4
       (elixir 1.18.4) lib/enum.ex:1714: Enum."-map/2-lists^map/1-1-"/2
       (ash 3.5.26) lib/ash/actions/read/relationships.ex:25: Ash.Actions.Read.Relationships.load/4
       (ash 3.5.26) lib/ash/actions/read/read.ex:358: Ash.Actions.Read.do_run/3
       (ash 3.5.26) lib/ash/actions/read/read.ex:86: anonymous fn/3 in Ash.Actions.Read.run/3
       (ash 3.5.26) lib/ash/actions/read/read.ex:85: Ash.Actions.Read.run/3
       (ash 3.5.26) lib/ash.ex:3002: Ash.do_read_one/3
       (ash 3.5.26) lib/ash.ex:2910: Ash.read_one/2
       (ash 3.5.26) lib/ash.ex:2863: Ash.read_one!/2

The desired behavior is to select all instances of Deployment which have at least one DeploymentTag matching the key and value field of the AlertRule.

Every help is appreciated. Thanks in advance!

Marked As Solved

pascal1

pascal1

Directly after posting it I figured it out. I’ve to “escape” twice to the parent resource.

    has_many :monitored_deployments, App.Deployments.Deployment do
      no_attributes? true
      filter expr(workspace_id == parent(workspace_id))

      filter expr(
               exists(
                 deployment_tags,
                 key == parent(parent(key)) and value == parent(parent(value))
               )
             )
    end

Also Liked

pascal1

pascal1

Good point with the composite index! Thanks for the tip.

Where Next?

Popular in Questions Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <%= ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29603 241
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement