** (KeyError) key :type not found in: {{:join_count, [], Ecto.Query.Builder.Join}, :on}. If you are using the dot syntax, such as map.field, make sure the left-hand side of the dot is a map

Dear Forum,

I’m encountering the following error and have been stumped for some time. Could you kindly inform me of my mistake? Thank you.

defp filter_brand_accessible_to_user_id(query, user_id) do
  query
  |> join(:inner, [b], m in assoc(b, :brand_members), on: m.user_id == ^user_id)
end

defp filter_project_to_brands_managed_by_user_id(query, user_id) do
  query
  |> join(
    :inner,
    [g, b],
    m in assoc(b, :brand_members),
    on: m.user_id == ^user_id and m.admin == true
  )
end

defp filter_project_to_active_and_enabled_brands(query) do
query
|> join(
  :inner,
  [g],
  b in assoc(g, :brand),
  on: is_nil(b.disabled_at) and is_nil(b.deleted_at)
)

end

== Compilation error in file lib/messenger/brands/brands.ex ==
** (KeyError) key :type not found in: {{:join_count, [], Ecto.Query.Builder.Join}, :on}. If you are using the dot syntax, such as map.field, make sure the left-hand side of the dot is a map
(ecto 2.1.6) lib/ecto/type.ex:233: Ecto.Type.match?/2
(ecto 2.1.6) lib/ecto/query/builder.ex:345: Ecto.Query.Builder.assert_type!/3
(ecto 2.1.6) lib/ecto/query/builder.ex:161: Ecto.Query.Builder.escape/5
(ecto 2.1.6) lib/ecto/query/builder/filter.ex:27: anonymous fn/7 in Ecto.Query.Builder.Filter.escape/5
(elixir 1.14.3) lib/enum.ex:1780: Enum.“-map_reduce/3-lists^mapfoldl/2-0-”/3
(ecto 2.1.6) lib/ecto/query/builder/filter.ex:22: Ecto.Query.Builder.Filter.escape/5
(ecto 2.1.6) lib/ecto/query/builder/join.ex:162: Ecto.Query.Builder.Join.build_on/9
(ecto 2.1.6) lib/ecto/query/builder/join.ex:145: Ecto.Query.Builder.Join.build/7

1 Like

The code you’ve posted looks reasonable, but it’s unclear how it is being used since the stack trace cuts off before any lines from application modules. Can you show how these functions are being called?

FWIW, the term from your KeyError looks like the AST generated for join_count here.

1 Like

Thank you for getting back. Below is a usage:

def project_show(%{id: user_id}, %{"id" => id}) do
  project =
    SearchGroup
    |> filter_project_by_id(id)
    |> filter_project_to_active_and_enabled_brands
    |> filter_project_accessible_to_user_id(user_id)
    |> Repo.one()

  case project do
    nil -> {:error, JsonApiErrors.not_found()}
    _ -> {:ok, project}
  end
end
1 Like

You’re still not showing a stack trace that contains reference to one of your functions. At one point it has to be called and visible in the stack trace e.g. lib/your_app/something.ex:17.

1 Like

The complete trace is shown below. I was searching for a specific line number as well but I couldn’t find one, hence the difficulty.

== Compilation error in file lib/messenger/brands/brands.ex ==
** (KeyError) key :type not found in: {{:join_count, [], Ecto.Query.Builder.Join}, :on}. If you are using the dot syntax, such as map.field, make sure the left-hand side of the dot is a map
(ecto 2.1.6) lib/ecto/type.ex:233: Ecto.Type.match?/2
(ecto 2.1.6) lib/ecto/query/builder.ex:345: Ecto.Query.Builder.assert_type!/3
(ecto 2.1.6) lib/ecto/query/builder.ex:161: Ecto.Query.Builder.escape/5
(ecto 2.1.6) lib/ecto/query/builder/filter.ex:27: anonymous fn/7 in Ecto.Query.Builder.Filter.escape/5
(elixir 1.14.3) lib/enum.ex:1780: Enum.“-map_reduce/3-lists^mapfoldl/2-0-”/3
(ecto 2.1.6) lib/ecto/query/builder/filter.ex:22: Ecto.Query.Builder.Filter.escape/5
(ecto 2.1.6) lib/ecto/query/builder/join.ex:162: Ecto.Query.Builder.Join.build_on/9
(ecto 2.1.6) lib/ecto/query/builder/join.ex:145: Ecto.Query.Builder.Join.build/7
surf@Surfers-MacBook-Air messenger %

Is this not just a heex error? The only time I get this message is with the following.

For example if I have the below everything will work fine because username exists within user
<%= @user.username %>

If I add username as another dot like this
<%= @user.username.username %>

I will get the following:

key :username not found in: “test”. If you are using the dot syntax, such as map.field, make sure the left-hand side of the dot is a map

The error lacked detail, but I found a solution. The root cause was a dependency.

Thank you so much for looking into it.

Wishing you all lots of positive energy!