mbrg

mbrg

Using a `no_attributes` relationship in a calculation

Hi,

I have recently finished the Ash Framework book ,and would like to add some more features to my Tunez app, starting with a friend system.

What I came up with was to model everything with a FriendRequest resource between a source and target user, like in this blog post. A user has (accepted) friend requests, a user Alice is friends with Bob if Alice’s ID appears in Bob’s list of accepted friend requests as either the target or the source.

So far I have been able to set up friendships between users and to list their friends. What I want to do now is to add an is_my_friend calculation which works similarly to the followed_by_me calculation on Artist.

My resources look like this (just the relevant bits, the Tunez resource also has account registration etc.):

defmodule FriendshipTest.Accounts.User do
  # ... snip
  attributes do
    uuid_primary_key :id

    attribute :username, :ci_string do
      allow_nil? false
      public? true
    end
  end

  relationships do
    has_many :friends, FriendshipTest.Accounts.User do
      no_attributes? true
      public? true

      filter expr(
               parent(accepted_friend_requests.target_user_id) in [id, parent(id)] &&
                 parent(accepted_friend_requests.source_user_id) in [id, parent(id)]
             )
    end

    has_many :accepted_friend_requests, FriendshipTest.Accounts.FriendRequest do
      public? true
      no_attributes? true

      filter expr(
               parent(id) in [source_user_id, target_user_id] &&
                 not pending
             )
    end
  end

  calculations do
    calculate :is_my_friend, :boolean, expr(exists(friends, id == ^actor(:id))) do
      public? true
    end
  end
  # ... snip
end

defmodule FriendshipTest.Accounts.FriendRequest do
  # ... snip
  attributes do
    create_timestamp :inserted_at

    attribute :accepted_at, :utc_datetime_usec do
      public? true
    end
  end

  relationships do
    belongs_to :source_user, FriendshipTest.Accounts.User do
      public? true
      allow_nil? false
      primary_key? true
    end

    belongs_to :target_user, FriendshipTest.Accounts.User do
      public? true
      allow_nil? false
      primary_key? true
    end
  end

  calculations do
    calculate :pending, :boolean, expr(is_nil(accepted_at))
  end
  # ...snip
end

This works for befriending users and listing their friends:

iex(1)> [user_a, user_b] = Friendshiptest.Repo.all(FriendshipTest.Accounts.User)
iex(2)> FriendshipTest.Accounts.befriend_user!(user_a, actor: user_b)
iex(3)> FriendshipTest.Accounts.befriend_user!(user_b, actor: user_a)

iex(4)> Ash.load!(user_a, :friends)
# ... database query
%FriendshipTest.Accounts.User{
  id: "48a91eeb-72ab-4161-af63-68742739c58f",
  username: #Ash.CiString<"user_a">,
  is_my_friend: #Ash.NotLoaded<:calculation, field: :is_my_friend>,
  friends: [
    %FriendshipTest.Accounts.User{
      id: "8a5a1841-9c3d-4195-8877-f64c0826f7c7",
      username: #Ash.CiString<"user_b">,
      is_my_friend: #Ash.NotLoaded<:calculation, field: :is_my_friend>,
      friends: #Ash.NotLoaded<:relationship, field: :friends>,
      accepted_friend_requests: #Ash.NotLoaded<:relationship, field: :accepted_friend_requests>,
      __meta__: #Ecto.Schema.Metadata<:loaded, "public", "users">
    }
  ],
  accepted_friend_requests: #Ash.NotLoaded<:relationship, field: :accepted_friend_requests>,
  __meta__: #Ecto.Schema.Metadata<:loaded, "users">
}

iex(5)> Ash.load!(user_b, :friends)
#...
%FriendshipTest.Accounts.User{
  id: "8a5a1841-9c3d-4195-8877-f64c0826f7c7",
  username: #Ash.CiString<"user_b">,
  is_my_friend: #Ash.NotLoaded<:calculation, field: :is_my_friend>,
  friends: [
    %FriendshipTest.Accounts.User{
      id: "48a91eeb-72ab-4161-af63-68742739c58f",
      username: #Ash.CiString<"user_a">,
      is_my_friend: #Ash.NotLoaded<:calculation, field: :is_my_friend>,
      friends: #Ash.NotLoaded<:relationship, field: :friends>,
      accepted_friend_requests: #Ash.NotLoaded<:relationship, field: :accepted_friend_requests>,
      __meta__: #Ecto.Schema.Metadata<:loaded, "public", "users">
    }
  ],
  accepted_friend_requests: #Ash.NotLoaded<:relationship, field: :accepted_friend_requests>,
  __meta__: #Ecto.Schema.Metadata<:loaded, "users">
}

But when I try to load is_my_friend I get this error:

iex(6)> Ash.load!(user_a, :is_my_friend, actor: user_b)
** (Ash.Error.Unknown)
Bread Crumbs:
  > Exception raised in: FriendshipTest.Accounts.User.read

Unknown Error

* ** (RuntimeError) Error while building parent reference: accepted_friend_requests.target_user_id

Query so far:

#Ecto.Query<from u0 in FriendshipTest.Accounts.User, as: 1, select: struct(u0, [:id, :username])>

Current bindings:

%{0 => %{type: :root, path: [], source: FriendshipTest.Accounts.User}}

  (ash_sql 0.2.86) lib/expr.ex:3302: AshSql.Expr.reference_error!/2
  (ash_sql 0.2.86) lib/expr.ex:2283: AshSql.Expr.default_dynamic_expr/6
  (ash_sql 0.2.86) lib/expr.ex:1851: AshSql.Expr.default_dynamic_expr/6
  (ash_sql 0.2.86) lib/expr.ex:1201: AshSql.Expr.default_dynamic_expr/6
  (ash_sql 0.2.86) lib/expr.ex:1851: AshSql.Expr.default_dynamic_expr/6
  (ash_sql 0.2.86) lib/expr.ex:1201: AshSql.Expr.default_dynamic_expr/6
  (ash_sql 0.2.86) lib/filter.ex:38: anonymous fn/2 in AshSql.Filter.add_filter_expression/2
  (elixir 1.18.4) lib/enum.ex:2546: Enum."-reduce/3-lists^foldl/2-0-"/3
    (ash_sql 0.2.86) lib/expr.ex:3302: AshSql.Expr.reference_error!/2
    (ash_sql 0.2.86) lib/expr.ex:2283: AshSql.Expr.default_dynamic_expr/6
    (ash_sql 0.2.86) lib/expr.ex:1851: AshSql.Expr.default_dynamic_expr/6
    (ash_sql 0.2.86) lib/expr.ex:1201: AshSql.Expr.default_dynamic_expr/6
    (ash_sql 0.2.86) lib/expr.ex:1851: AshSql.Expr.default_dynamic_expr/6
    (ash_sql 0.2.86) lib/expr.ex:1201: AshSql.Expr.default_dynamic_expr/6
    (ash_sql 0.2.86) lib/filter.ex:38: anonymous fn/2 in AshSql.Filter.add_filter_expression/2
    iex:6: (file)

Am I missing something obvious?

I have also put the code on GitHub, along with a failing testcase if that helps.

First Post!

zachdaniel

zachdaniel

Creator of Ash

This is definitely a bug. Please open an issue and I’ll investigate early this week.

Most Liked

mbrg

mbrg

https://github.com/ash-project/ash/issues/2214

Thanks for the quick reply, lease let me know if I need to change anything.

Where Next?

Popular in Questions Top

ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
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
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
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics Top

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
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

We're in Beta

About us Mission Statement