ecto icon indicating copy to clipboard operation
ecto copied to clipboard

Getting "key :join_where not found in: %{__struct__: Ecto.Association.ManyToMany.." when doing join query

Open phasnox opened this issue 6 years ago • 3 comments

Environment

  • Elixir version (elixir -v): 1.10.3
  • Ecto version (mix deps):
  • Database adapter and version (mix deps):

ecto 3.4.5 (Hex package) (mix) locked at 3.4.5 (ecto) 8c6d1d4d ecto_sql 3.4.4 (Hex package) (mix) locked at 3.4.4 (ecto_sql) edb49af7 phoenix_ecto 4.1.0 (Hex package) (mix) locked at 4.1.0 (phoenix_ecto) c5e666a3

  • Operating system: Linux

Current behavior

Getting the following error:

key :join_where not found in: %{__struct__: Ecto.Association.ManyToMany, cardinality: :many, defaults: [], field: :generators, join_defaults: [], join_keys: [tag_id: :id, generator_id: :id], join_through: "generator_tags", on_cast: nil, on_delete: :nothing, on_replace: :raise, ordered: false, owner: Tag, owner_key: :id, queryable: SomeObject, related: SomeObject, relationship: :child, unique: false, where: []}

Trace:

lib/ecto/association.ex
    from(o in owner,
      join: j in ^join_through, on: field(j, ^join_owner_key) == field(o, ^owner_key),
      join: q in ^queryable, on: field(j, ^join_related_key) == field(q, ^related_key))
    |> Ecto.Association.combine_joins_query(assoc.where, 2)
    |> Ecto.Association.combine_joins_query(assoc.join_where, 1)
  end
  @doc false
  def assoc_query(%{queryable: queryable} = refl, values) do
    assoc_query(refl, queryable, values)
Ecto.Association.ManyToMany.joins_query/1

When doing:

    Repo.all(
      from t in Tag,
        join: g in assoc(t, :some_objects),
        join: a in assoc(t, :some_other_objects),
        where: a.entity_id == ^obj.entity_id or g.entity_id == ^obj.entity_id,
        limit: ^max_results,
        group_by: t.id,
        order_by: [desc: :count],
        select: %{name: t.name, count: count(t.id)}
    )

Expected behavior

This exact same query is working in the following versions:

  • ecto 3.4.3 (Hex package) (mix) locked at 3.4.3 (ecto) 9b6f18de
  • ecto_sql 3.4.3 (Hex package) (mix) locked at 3.4.3 (ecto_sql) ec9e59d6
  • phoenix_ecto 4.1.0 (Hex package) (mix) locked at 4.1.0 (phoenix_ecto) c5e666a3

phasnox avatar Jun 29 '20 16:06 phasnox

Can you please provide a sample app that reproduces the error? Thank you!

josevalim avatar Jun 29 '20 16:06 josevalim

Actually, remove “deps” and _build and it should be fixed. As you can see, the field is there: https://github.com/elixir-ecto/ecto/blob/master/lib/ecto/association.ex#L1009 but for some reason you have an old version of the struct around. :) if that doesn’t fix it, then we will need a way to reproduce it.

josevalim avatar Jun 29 '20 16:06 josevalim

That did the trick!

Thanks @josevalim !

phasnox avatar Jun 29 '20 22:06 phasnox