From f2b6a9ca1fe5d9658d3261cea07ddeb0d69bba27 Mon Sep 17 00:00:00 2001 From: Artur Plysyuk Date: Fri, 2 Aug 2024 17:10:19 +0300 Subject: [PATCH] Generate id for schemas without primary key handles a case when embedded schema struct belongs to a regular record --- lib/dataloader/ecto.ex | 10 ++++++- mix.exs | 3 ++- mix.lock | 1 + priv/test_repo/migrations/1_migrate_all.exs | 2 +- test/dataloader/ecto/belongs_to_test.exs | 29 ++++++++++++++++++++- test/support/address.ex | 9 +++++++ test/support/country.ex | 7 +++++ 7 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 test/support/address.ex create mode 100644 test/support/country.ex diff --git a/lib/dataloader/ecto.ex b/lib/dataloader/ecto.ex index 33406b6..16cff07 100644 --- a/lib/dataloader/ecto.ex +++ b/lib/dataloader/ecto.ex @@ -512,7 +512,15 @@ if Code.ensure_loaded?(Ecto) do defp get_keys({assoc_field, opts}, %schema{} = record) when is_atom(assoc_field) do validate_queryable(schema) primary_keys = schema.__schema__(:primary_key) - id = Enum.map(primary_keys, &Map.get(record, &1)) + + id = + if primary_keys == [] do + record + |> :erlang.term_to_iovec() + |> :erlang.md5() + else + Enum.map(primary_keys, &Map.get(record, &1)) + end queryable = chase_down_queryable([assoc_field], schema) diff --git a/mix.exs b/mix.exs index a00f49e..5938a10 100644 --- a/mix.exs +++ b/mix.exs @@ -67,7 +67,8 @@ defmodule Dataloader.Mixfile do {:ecto_sql, "~> 3.0", optional: true, only: :test}, {:postgrex, "~> 0.14", only: :test, runtime: false}, {:dialyxir, "~> 1.3.0", only: [:dev, :test], runtime: false}, - {:ex_doc, "~> 0.24", only: :dev, runtime: false} + {:ex_doc, "~> 0.24", only: :dev, runtime: false}, + {:jason, "~> 1.0", only: :test} ] end diff --git a/mix.lock b/mix.lock index 999a182..8cbe925 100644 --- a/mix.lock +++ b/mix.lock @@ -8,6 +8,7 @@ "ecto_sql": {:hex, :ecto_sql, "3.10.1", "6ea6b3036a0b0ca94c2a02613fd9f742614b5cfe494c41af2e6571bb034dd94c", [:mix], [{:db_connection, "~> 2.4.1 or ~> 2.5", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.10.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.6.0", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.16.0 or ~> 0.17.0 or ~> 1.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:tds, "~> 2.1.1 or ~> 2.2", [hex: :tds, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "f6a25bdbbd695f12c8171eaff0851fa4c8e72eec1e98c7364402dda9ce11c56b"}, "erlex": {:hex, :erlex, "0.2.6", "c7987d15e899c7a2f34f5420d2a2ea0d659682c06ac607572df55a43753aa12e", [:mix], [], "hexpm", "2ed2e25711feb44d52b17d2780eabf998452f6efda104877a3881c2f8c0c0c75"}, "ex_doc": {:hex, :ex_doc, "0.29.4", "6257ecbb20c7396b1fe5accd55b7b0d23f44b6aa18017b415cb4c2b91d997729", [:mix], [{:earmark_parser, "~> 1.4.31", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "2c6699a737ae46cb61e4ed012af931b57b699643b24dabe2400a8168414bc4f5"}, + "jason": {:hex, :jason, "1.4.4", "b9226785a9aa77b6857ca22832cffa5d5011a667207eb2a0ad56adb5db443b8a", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "c5eb0cab91f094599f94d55bc63409236a8ec69a21a67814529e8d5f6cc90b3b"}, "makeup": {:hex, :makeup, "1.1.0", "6b67c8bc2882a6b6a445859952a602afc1a41c2e08379ca057c0f525366fc3ca", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "0a45ed501f4a8897f580eabf99a2e5234ea3e75a4373c8a52824f6e873be57a6"}, "makeup_elixir": {:hex, :makeup_elixir, "0.16.1", "cc9e3ca312f1cfeccc572b37a09980287e243648108384b97ff2b76e505c3555", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "e127a341ad1b209bd80f7bd1620a15693a9908ed780c3b763bccf7d200c767c6"}, "makeup_erlang": {:hex, :makeup_erlang, "0.1.2", "ad87296a092a46e03b7e9b0be7631ddcf64c790fa68a9ef5323b6cbb36affc72", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "f3f5a1ca93ce6e092d92b6d9c049bcda58a3b617a8d888f8e7231c85630e8108"}, diff --git a/priv/test_repo/migrations/1_migrate_all.exs b/priv/test_repo/migrations/1_migrate_all.exs index fb04df4..8f70f63 100644 --- a/priv/test_repo/migrations/1_migrate_all.exs +++ b/priv/test_repo/migrations/1_migrate_all.exs @@ -1,4 +1,4 @@ -defmodule Absinthe.Ecto.TestRepo.Migrations.MigrateAll do +defmodule Dataloader.Ecto.TestRepo.Migrations.MigrateAll do use Ecto.Migration def change do diff --git a/test/dataloader/ecto/belongs_to_test.exs b/test/dataloader/ecto/belongs_to_test.exs index 711beb4..1c04453 100644 --- a/test/dataloader/ecto/belongs_to_test.exs +++ b/test/dataloader/ecto/belongs_to_test.exs @@ -1,7 +1,7 @@ defmodule Dataloader.Ecto.BelongsToTest do use ExUnit.Case, async: true - alias Dataloader.{User, Post, Like} + alias Dataloader.{User, Post, Like, Country, Address} import Ecto.Query alias Dataloader.TestRepo, as: Repo @@ -30,6 +30,11 @@ defmodule Dataloader.Ecto.BelongsToTest do |> limit(^limit) end + defp query(queryable, _args, test_pid) do + send(test_pid, :querying) + queryable + end + describe "where in belongs_to association" do test "returns entity when where clause matches", %{loader: loader} do user1 = %User{username: "Ben Wilson"} |> Repo.insert!() @@ -65,4 +70,26 @@ defmodule Dataloader.Ecto.BelongsToTest do assert nil == Dataloader.get(loader, Test, args, like1) end end + + test "belongs_to is inside the embedded schema", %{loader: loader} do + country1 = %Country{name: "USA"} |> Repo.insert!() + country2 = %Country{name: "Canada"} |> Repo.insert!() + + address1 = %Address{city: "New York", country_id: country1.id} + address2 = %Address{city: "Toronto", country_id: country2.id} + address3 = %Address{city: "Washington"} + + args = {:country, %{}} + + loader = + loader + |> Dataloader.load(Test, args, address1) + |> Dataloader.load(Test, args, address2) + |> Dataloader.load(Test, args, address3) + |> Dataloader.run() + + assert country1 == Dataloader.get(loader, Test, args, address1) + assert country2 == Dataloader.get(loader, Test, args, address2) + assert nil == Dataloader.get(loader, Test, args, address3) + end end diff --git a/test/support/address.ex b/test/support/address.ex new file mode 100644 index 0000000..f79ca82 --- /dev/null +++ b/test/support/address.ex @@ -0,0 +1,9 @@ +defmodule Dataloader.Address do + use Ecto.Schema + + @primary_key false + embedded_schema do + field(:city, :string) + belongs_to(:country, Dataloader.Country) + end +end diff --git a/test/support/country.ex b/test/support/country.ex new file mode 100644 index 0000000..05d5f6d --- /dev/null +++ b/test/support/country.ex @@ -0,0 +1,7 @@ +defmodule Dataloader.Country do + use Ecto.Schema + + schema "countries" do + field(:name, :string) + end +end