{"action":"created","issue":{"url":"https://api.github.com/repos/elixir-lang/ecto/issues/573","labels_url":"https://api.github.com/repos/elixir-lang/ecto/issues/573/labels{/name}","comments_url":"https://api.github.com/repos/elixir-lang/ecto/issues/573/comments","events_url":"https://api.github.com/repos/elixir-lang/ecto/issues/573/events","html_url":"https://github.com/elixir-lang/ecto/issues/573","id":71868843,"number":573,"title":"Ecto.Repo.one ignores the \"where\" from an Ecto.Query","user":{"login":"jeznet","id":35540,"avatar_url":"https://avatars.githubusercontent.com/u/35540?v=3","gravatar_id":"","url":"https://api.github.com/users/jeznet","html_url":"https://github.com/jeznet","followers_url":"https://api.github.com/users/jeznet/followers","following_url":"https://api.github.com/users/jeznet/following{/other_user}","gists_url":"https://api.github.com/users/jeznet/gists{/gist_id}","starred_url":"https://api.github.com/users/jeznet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jeznet/subscriptions","organizations_url":"https://api.github.com/users/jeznet/orgs","repos_url":"https://api.github.com/users/jeznet/repos","events_url":"https://api.github.com/users/jeznet/events{/privacy}","received_events_url":"https://api.github.com/users/jeznet/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-04-29T13:00:29Z","updated_at":"2015-04-29T14:41:28Z","closed_at":"2015-04-29T14:41:28Z","body":"For a test case like this:\r\n```\r\ndefmodule MyApp.CommentTest do\r\n  use ExUnit.Case, async: false\r\n\r\n  test \"insert, get\" do\r\n    comment = %MyApp.Comment{\r\n      web_content_id: 10,\r\n      content: \"test\",\r\n      published_at: {{2015, 04, 28}, {08, 25, 58, 0}},\r\n    }\r\n    assert %MyApp.Comment{} = Repo.insert(comment)\r\n\r\n    comment = %MyApp.Comment{\r\n      web_content_id: 11,\r\n      content: \"some test\",\r\n      published_at: {{2015, 04, 28}, {08, 25, 59, 0}},\r\n    }\r\n    assert %MyApp.Comment{} = Repo.insert(comment)\r\n\r\n    q = from c in MyApp.Comment,\r\n      where: c.content == \"test\"\r\n    comment = Repo.one!(q)\r\n    assert %MyApp.Comment{ web_content_id: 10 } = comment\r\n  end\r\nend\r\n```\r\nrunning `mix test` gives:\r\n```\r\n  1) test insert, get, update, delete (Ehnix.CommentTest)\r\n     test/my_app/models/comment_test.exs:6\r\n     ** (Ecto.MultipleResultsError) expected at most one result but got 2 in query:\r\n     \r\n     from c in Ehnix.Comment\r\n     \r\n     stacktrace:\r\n       (ecto) lib/ecto/repo/queryable.ex:46: Ecto.Repo.Queryable.one/4\r\n       test/ehnix/models/comment2_test.exs:28\r\n```\r\nThe where clause seems to disappear...\r\n\r\nAfter changing the query to:\r\n```\r\n    q = from c in MyApp.Comment,\r\n      where: c.content == \"not  existing text\r\n```\r\nit gets:\r\n```\r\n     ** (Ecto.NoResultsError) expected at least one result but got none in query:\r\n     \r\n     from c in Ehnix.Comment,\r\n       where: c.content == \"non existing text\"\r\n```\r\nso the where part appears.\r\n\r\nStrangely this code works for me in IEx.\r\n\r\nVersions:\r\n```\r\n$ cat mix.lock | grep ecto\r\n  \"ecto\": {:hex, :ecto, \"0.10.2\"},\r\n$ elixir -v\r\nElixir 1.0.4\r\n$ psql --version\r\npsql (PostgreSQL) 9.3.6\r\n```"},"comment":{"url":"https://api.github.com/repos/elixir-lang/ecto/issues/comments/97452116","html_url":"https://github.com/elixir-lang/ecto/issues/573#issuecomment-97452116","issue_url":"https://api.github.com/repos/elixir-lang/ecto/issues/573","id":97452116,"user":{"login":"jeznet","id":35540,"avatar_url":"https://avatars.githubusercontent.com/u/35540?v=3","gravatar_id":"","url":"https://api.github.com/users/jeznet","html_url":"https://github.com/jeznet","followers_url":"https://api.github.com/users/jeznet/followers","following_url":"https://api.github.com/users/jeznet/following{/other_user}","gists_url":"https://api.github.com/users/jeznet/gists{/gist_id}","starred_url":"https://api.github.com/users/jeznet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jeznet/subscriptions","organizations_url":"https://api.github.com/users/jeznet/orgs","repos_url":"https://api.github.com/users/jeznet/repos","events_url":"https://api.github.com/users/jeznet/events{/privacy}","received_events_url":"https://api.github.com/users/jeznet/received_events","type":"User","site_admin":false},"created_at":"2015-04-29T14:41:28Z","updated_at":"2015-04-29T14:41:28Z","body":"Issue resolved. I had overlooked something and posted only a part of my code to the example above. Some code down the line which was using Repo.one(Comment) not Repo.one(q) was causing the error message (of course then without the where clause) and confused me.\r\nSorry for posting an incomplete example and bothering you.\r\n\r\nHere is the output:\r\n```\r\n$ mix test                                                                                                                                                                       \r\n..\r\n$ mix test\r\n\r\n  1) test insert, get (MyApp.CommentTest)\r\n     test/ehnix/models/comment2_test.exs:6\r\n     ** (Ecto.MultipleResultsError) expected at most one result but got 2 in query:\r\n     \r\n     from c in Comment\r\n     \r\n     stacktrace:\r\n       (ecto) lib/ecto/repo/queryable.ex:46: Ecto.Repo.Queryable.one/4\r\n       test/ehnix/models/comment_test.exs:29  # => code with Repo.one(Comment)\r\n\r\n# Second run from the example above:\r\n\r\n  1) test insert, get (MyApp.CommentTest)\r\n     test/ehnix/models/comment_test.exs:6\r\n     ** (Ecto.NoResultsError) expected at least one result but got none in query:\r\n     \r\n     from c in Comment,\r\n       where: c.content == \"non existing text\"\r\n     \r\n     stacktrace:\r\n       (ecto) lib/ecto/repo/queryable.ex:56: Ecto.Repo.Queryable.one!/4\r\n       test/ehnix/models/comment_test.exs:23 # different line (not 29)\r\n```\r\n\r\nAnyway maybe that could be a good idea to write somewhere in the docu that `Repo.one(Comment)` expands to:Repo.one(from c in Comment)`, or is it already somewhere?"}}