Ash FrameworkAF
Ash Framework3y ago
32 replies
drumusician

many_to_many with non-default attribute not preloading...

So, I have a many to many relationship I'm strugling with. It must be something silly, but I just can't make it work.

I have a resource called Game, which is actually for a table called assets, so in the join table one of the attributes is asset_id and not game_id.

The join table is called playlist_entries

Any help would be greatly appreciated. Basically it is not loading the games correctly for the playlist. Addng the full resources made the post too long, so hopefully this will shed enough light...

Playlist
defmodule McFun.Games.Playlist do
  use Ash.Resource,
    data_layer: AshPostgres.DataLayer,
    extensions: [
      AshGraphql.Resource
    ]

  alias McFun.Games.PlaylistEntry
  alias McFun.Games.Game

  postgres do
    schema("games")
    repo(McCore.Repo)
    table("playlists")
  end

  relationships do
    many_to_many :games, Game do
      through(PlaylistEntry)
      source_attribute_on_join_resource(:playlist_id)
      destination_attribute_on_join_resource(:asset_id)
    end
  end
end


PlaylistEntry
  postgres do
    schema("games")
    repo(McCore.Repo)
    table("playlist_entries")
  end

  relationships do
    belongs_to :playlist, Playlist do
      source_attribute(:playlist_id)
      primary_key?(true)
      allow_nil?(false)
      attribute_type(:integer)
    end

    belongs_to :game, Game do
      source_attribute(:asset_id)
      primary_key?(true)
      allow_nil?(false)
      attribute_type(:integer)
    end


Game
  postgres do
    schema("games")
    repo(McCore.Repo)
    table("assets")
  end
end
Was this page helpful?