From 1c354b33b71f74931299aeed4e2bd71c62c2d0f3 Mon Sep 17 00:00:00 2001 From: Rebecca Le Date: Mon, 12 Oct 2020 09:50:51 +0800 Subject: [PATCH] Use the same database prefix as the existing rows when preloading via a lateral join --- lib/dataloader/ecto.ex | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/dataloader/ecto.ex b/lib/dataloader/ecto.ex index 0f3901e..bc4e14f 100644 --- a/lib/dataloader/ecto.ex +++ b/lib/dataloader/ecto.ex @@ -673,9 +673,12 @@ if Code.ensure_loaded?(Ecto) do def preload_lateral([], _assoc, _query, _opts), do: [] - def preload_lateral([%schema{} | _] = structs, assoc, query, repo, repo_opts) do + def preload_lateral([%schema{} = struct | _] = structs, assoc, query, repo, repo_opts) do [pk] = schema.__schema__(:primary_key) + # Carry the database prefix across from already-loaded records if not already set + repo_opts = Keyword.put_new(repo_opts, :prefix, struct.__meta__.prefix) + assocs = expand_assocs(schema, [assoc]) inner_query =