{"action":"created","issue":{"url":"https://api.github.com/repos/elixir-lang/ecto/issues/1284","repository_url":"https://api.github.com/repos/elixir-lang/ecto","labels_url":"https://api.github.com/repos/elixir-lang/ecto/issues/1284/labels{/name}","comments_url":"https://api.github.com/repos/elixir-lang/ecto/issues/1284/comments","events_url":"https://api.github.com/repos/elixir-lang/ecto/issues/1284/events","html_url":"https://github.com/elixir-lang/ecto/issues/1284","id":137093889,"number":1284,"title":"Issue when composing ecto queries","user":{"login":"iwarshak","id":471,"avatar_url":"https://avatars.githubusercontent.com/u/471?v=3","gravatar_id":"","url":"https://api.github.com/users/iwarshak","html_url":"https://github.com/iwarshak","followers_url":"https://api.github.com/users/iwarshak/followers","following_url":"https://api.github.com/users/iwarshak/following{/other_user}","gists_url":"https://api.github.com/users/iwarshak/gists{/gist_id}","starred_url":"https://api.github.com/users/iwarshak/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/iwarshak/subscriptions","organizations_url":"https://api.github.com/users/iwarshak/orgs","repos_url":"https://api.github.com/users/iwarshak/repos","events_url":"https://api.github.com/users/iwarshak/events{/privacy}","received_events_url":"https://api.github.com/users/iwarshak/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2016-02-28T20:42:10Z","updated_at":"2016-03-02T22:17:16Z","closed_at":null,"body":"\r\n### Environment\r\n\r\n* Elixir version (elixir -v): 1.1.1\r\n* Database and version (PostgreSQL 9.4, MongoDB 3.2, etc.): Postgresql\r\n* Ecto version (mix deps): 1.1.4\r\n* Database adapter and version (mix deps):  postgrex 0.11.1\r\n* Operating system: mac\r\n\r\nI am trying to write a set of composable queries. I am running into an issue when composing 2 queries where each query joins a separate table, and the second query has a where condition. \r\n\r\n\r\n```\r\n# comment.ex\r\n\r\n  schema \"comments\" do\r\n    field :title, :string\r\n    field :body, :string\r\n    field :likes, :integer\r\n    belongs_to :author, HelloPhoenix.Author\r\n    belongs_to :post, HelloPhoenix.Post\r\n\r\n    timestamps\r\n  end\r\n\r\n  def with_popular_post(query) do\r\n    query\r\n    |> join(:inner, [comment], p in assoc(comment, :post))\r\n    |> where([comment, p],  p.likes == 4)\r\n  end\r\n\r\n  def for_author(query, author) do\r\n    query\r\n    |> join(:inner, [comment], a in assoc(comment, :author))\r\n    |> where([comment, a],  a.id == ^author.id)\r\n  end\r\n\r\n```\r\n\r\n```\r\n\r\nComment |> Comment.for_author(author) |> Comment.with_popular_post() |> Repo.all\r\n\r\n     ** (Ecto.QueryError) web/models/comment.ex:21: field `HelloPhoenix.Author.likes` in `where` does not exist in the model source in query:\r\n     \r\n     from c in HelloPhoenix.Comment,\r\n       join: a in HelloPhoenix.Author,\r\n       on: a.id == c.author_id,\r\n       join: p in HelloPhoenix.Post,\r\n       on: p.id == c.post_id,\r\n       where: a.id == ^28,\r\n       where: a.likes == 4\r\n```\r\n\r\nIt seems to me that the root of the issue is that there is another binding available after the first query runs, and the second query having no idea about it does the `where` on the incorrect binding.\r\n\r\nThis would seem to make it difficult to compose queries, since they would need to know of all the previously joined tables.\r\n\r\nAny ideas?\r\n\r\nFull source with failing test. https://github.com/iwarshak/ecto_compose_error\r\n"},"comment":{"url":"https://api.github.com/repos/elixir-lang/ecto/issues/comments/191457548","html_url":"https://github.com/elixir-lang/ecto/issues/1284#issuecomment-191457548","issue_url":"https://api.github.com/repos/elixir-lang/ecto/issues/1284","id":191457548,"user":{"login":"iwarshak","id":471,"avatar_url":"https://avatars.githubusercontent.com/u/471?v=3","gravatar_id":"","url":"https://api.github.com/users/iwarshak","html_url":"https://github.com/iwarshak","followers_url":"https://api.github.com/users/iwarshak/followers","following_url":"https://api.github.com/users/iwarshak/following{/other_user}","gists_url":"https://api.github.com/users/iwarshak/gists{/gist_id}","starred_url":"https://api.github.com/users/iwarshak/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/iwarshak/subscriptions","organizations_url":"https://api.github.com/users/iwarshak/orgs","repos_url":"https://api.github.com/users/iwarshak/repos","events_url":"https://api.github.com/users/iwarshak/events{/privacy}","received_events_url":"https://api.github.com/users/iwarshak/received_events","type":"User","site_admin":false},"created_at":"2016-03-02T22:17:16Z","updated_at":"2016-03-02T22:17:16Z","body":"Thanks @parkerl - @josevalim asked me to make this into an issue after discussing it in IRC."}}