From db55b0cba6525c24ebddc88ef9ae0c1c00620250 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sun, 27 Aug 2017 18:46:25 +0200 Subject: [PATCH] Revert "Add support for clauses with nil values in `Repo.get_by(!)/2` (#2125)" This reverts commit 8e1b378a6f79b0f26495578e57f896d3361c808a. --- integration_test/cases/repo.exs | 3 --- lib/ecto/repo/queryable.ex | 7 +------ test/ecto/repo_test.exs | 1 - 3 files changed, 1 insertion(+), 10 deletions(-) diff --git a/integration_test/cases/repo.exs b/integration_test/cases/repo.exs index d743a9d48a..0fa8c5b42e 100644 --- a/integration_test/cases/repo.exs +++ b/integration_test/cases/repo.exs @@ -601,7 +601,6 @@ defmodule Ecto.Integration.RepoTest do test "get_by(!)" do post1 = TestRepo.insert!(%Post{title: "1", text: "hai"}) post2 = TestRepo.insert!(%Post{title: "2", text: "hello"}) - post3 = TestRepo.insert!(%Post{title: "3", text: nil}) assert post1 == TestRepo.get_by(Post, id: post1.id) assert post1 == TestRepo.get_by(Post, text: post1.text) @@ -609,13 +608,11 @@ defmodule Ecto.Integration.RepoTest do assert post2 == TestRepo.get_by(Post, id: to_string(post2.id)) # With casting assert nil == TestRepo.get_by(Post, text: "hey") assert nil == TestRepo.get_by(Post, id: post2.id, text: "hey") - assert post3 == TestRepo.get_by(Post, text: nil) assert post1 == TestRepo.get_by!(Post, id: post1.id) assert post1 == TestRepo.get_by!(Post, text: post1.text) assert post1 == TestRepo.get_by!(Post, id: post1.id, text: post1.text) assert post2 == TestRepo.get_by!(Post, id: to_string(post2.id)) # With casting - assert post3 == TestRepo.get_by!(Post, text: nil) assert post1 == TestRepo.get_by!(Post, %{id: post1.id}) diff --git a/lib/ecto/repo/queryable.ex b/lib/ecto/repo/queryable.ex index 2337d8f8d9..848156120e 100644 --- a/lib/ecto/repo/queryable.ex +++ b/lib/ecto/repo/queryable.ex @@ -347,12 +347,7 @@ defmodule Ecto.Repo.Queryable do end defp query_for_get_by(_repo, queryable, clauses) do - Enum.reduce(clauses, queryable, fn - ({key, nil}, query) -> - Query.where(query, [x], is_nil(field(x, ^key))) - ({key, value}, query) -> - Query.where(query, [x], field(x, ^key) == ^value) - end) + Query.where(queryable, [], ^Enum.to_list(clauses)) end defp query_for_aggregate(queryable, aggregate, field) do diff --git a/test/ecto/repo_test.exs b/test/ecto/repo_test.exs index 97e9ea4672..837c415463 100644 --- a/test/ecto/repo_test.exs +++ b/test/ecto/repo_test.exs @@ -112,7 +112,6 @@ defmodule Ecto.RepoTest do test "validates get_by" do TestRepo.get_by(MySchema, id: 123) TestRepo.get_by(MySchema, %{id: 123}) - TestRepo.get_by(MySchema, id: nil) message = ~r"value `:atom` in `where` cannot be cast to type :id in query" assert_raise Ecto.Query.CastError, message, fn ->