Skip to content

Commit db55b0c

Browse files
author
José Valim
committed
Revert "Add support for clauses with nil values in Repo.get_by(!)/2 (#2125)"
This reverts commit 8e1b378.
1 parent f8f3c1f commit db55b0c

File tree

3 files changed

+1
-10
lines changed

3 files changed

+1
-10
lines changed

integration_test/cases/repo.exs

-3
Original file line numberDiff line numberDiff line change
@@ -601,21 +601,18 @@ defmodule Ecto.Integration.RepoTest do
601601
test "get_by(!)" do
602602
post1 = TestRepo.insert!(%Post{title: "1", text: "hai"})
603603
post2 = TestRepo.insert!(%Post{title: "2", text: "hello"})
604-
post3 = TestRepo.insert!(%Post{title: "3", text: nil})
605604

606605
assert post1 == TestRepo.get_by(Post, id: post1.id)
607606
assert post1 == TestRepo.get_by(Post, text: post1.text)
608607
assert post1 == TestRepo.get_by(Post, id: post1.id, text: post1.text)
609608
assert post2 == TestRepo.get_by(Post, id: to_string(post2.id)) # With casting
610609
assert nil == TestRepo.get_by(Post, text: "hey")
611610
assert nil == TestRepo.get_by(Post, id: post2.id, text: "hey")
612-
assert post3 == TestRepo.get_by(Post, text: nil)
613611

614612
assert post1 == TestRepo.get_by!(Post, id: post1.id)
615613
assert post1 == TestRepo.get_by!(Post, text: post1.text)
616614
assert post1 == TestRepo.get_by!(Post, id: post1.id, text: post1.text)
617615
assert post2 == TestRepo.get_by!(Post, id: to_string(post2.id)) # With casting
618-
assert post3 == TestRepo.get_by!(Post, text: nil)
619616

620617
assert post1 == TestRepo.get_by!(Post, %{id: post1.id})
621618

lib/ecto/repo/queryable.ex

+1-6
Original file line numberDiff line numberDiff line change
@@ -347,12 +347,7 @@ defmodule Ecto.Repo.Queryable do
347347
end
348348

349349
defp query_for_get_by(_repo, queryable, clauses) do
350-
Enum.reduce(clauses, queryable, fn
351-
({key, nil}, query) ->
352-
Query.where(query, [x], is_nil(field(x, ^key)))
353-
({key, value}, query) ->
354-
Query.where(query, [x], field(x, ^key) == ^value)
355-
end)
350+
Query.where(queryable, [], ^Enum.to_list(clauses))
356351
end
357352

358353
defp query_for_aggregate(queryable, aggregate, field) do

test/ecto/repo_test.exs

-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ defmodule Ecto.RepoTest do
112112
test "validates get_by" do
113113
TestRepo.get_by(MySchema, id: 123)
114114
TestRepo.get_by(MySchema, %{id: 123})
115-
TestRepo.get_by(MySchema, id: nil)
116115

117116
message = ~r"value `:atom` in `where` cannot be cast to type :id in query"
118117
assert_raise Ecto.Query.CastError, message, fn ->

0 commit comments

Comments
 (0)