Flop: join_fields across table and association does not appear in result

Using Flop I want to return a list of tuples containing a Membership along with associated User.email

membership.ex:

  @derive {
    Flop.Schema,
    filterable: [:member_email],
    sortable: [:member_email],
    join_fields: [member_email: [binding: :joined_user, field: :email]]
  }

  @role_options ~w(admin member)a

  schema "orgs_memberships" do
    field :role, Ecto.Enum, values: @role_options
    field :is_inactive, :boolean, default: false
    ...
    belongs_to :user, User
    belongs_to :org, Org


  def all_by_org_joined(%Org{} = org) do
    joined = from(m in __MODULE__,
      join: u in assoc(m, :user),
      as: :joined_user,
      join: o in assoc(m, :org),
      on: o.id == ^org.id
    )

    dbg(joined)

    result = joined
    |> Markably.Repo.all()

    dbg(result)

result prints as but seems to be missing member_email:

result #=> [
  %Markably.Orgs.Membership{
    __meta__: #Ecto.Schema.Metadata<:loaded, "orgs_memberships">,
    id: "1eda8b14-bf24-6736-85c7-a1d5647b6bd8",
    role: :admin,
    is_inactive: false,
    user_id: "1eda8b14-be9c-6822-8ab9-61e2ff5b420b",
    user: #Ecto.Association.NotLoaded<association :user is not loaded>,
    org_id: "1eda8b14-bf22-6d6e-b325-ea3ac7994ddf",
    org: #Ecto.Association.NotLoaded<association :org is not loaded>,
    inserted_at: ~N[2023-02-09 19:38:15],
    updated_at: ~N[2023-02-09 19:38:15]
  },
  %Markably.Orgs.Membership{
...

Am I doing this right?

flop 0.19.0
ecto 3.9.4
elixir 1.14.2

regards, Michael

You’re missing a select. Flop only adds where and order by clauses, it won’t add a select for you.

Thanks for the response, it was helpful but there is one caveat: if I select like this I will be returning as a map, the Petal Data Table(Install) needs a struct(and also obviates the need for Flop, of course):

    joined = from(m in __MODULE__,
      join: u in assoc(m, :user),
      as: :joined_user,
      join: o in assoc(m, :org),
      on: o.id == ^org.id,
      select: %{id: m.id, first_name: u.first_name, last_name: u.last_name, email: u.email, is_inactive: m.is_inactive}
    )

How do I select into a Membership struct that has the member_email along with it?

You’re currently explicitly selecting a map, see Ecto.Query.API — Ecto v3.9.4 for some ways to select a struct.

Thanks, that helped.