Skip to content

Commit

Permalink
Make Repo.reload respect source (#4510)
Browse files Browse the repository at this point in the history
  • Loading branch information
martosaur authored Sep 26, 2024
1 parent b4ad5fc commit e3f038f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/ecto/repo/queryable.ex
Original file line number Diff line number Diff line change
Expand Up @@ -491,12 +491,12 @@ defmodule Ecto.Repo.Queryable do
assert_structs!(structs)

schema = head.__struct__
prefix = head.__meta__.prefix
%{prefix: prefix, source: source} = head.__meta__

case schema.__schema__(:primary_key) do
[pk] ->
keys = Enum.map(structs, &get_pk!(&1, pk))
query = Query.from(x in schema, where: field(x, ^pk) in ^keys)
query = Query.from(x in {source, schema}, where: field(x, ^pk) in ^keys)
%{query | prefix: prefix}

pks ->
Expand Down
6 changes: 6 additions & 0 deletions test/ecto/repo_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,12 @@ defmodule Ecto.RepoTest do
TestRepo.reload(struct_with_prefix)
assert_received {:all, %{prefix: %{key: :another}}}
end

test "respects source" do
struct_with_custom_source = put_meta(%MySchema{id: 2}, source: "custom_schema")
TestRepo.reload(struct_with_custom_source)
assert_received {:all, %{from: %{source: {"custom_schema", MySchema}}}}
end
end

defmodule DefaultOptionRepo do
Expand Down

0 comments on commit e3f038f

Please sign in to comment.