Skip to content

Commit

Permalink
Merge pull request #173 from fuelen/handle_schemas_without_primary_key
Browse files Browse the repository at this point in the history
Generate id for schemas without primary key
  • Loading branch information
benwilson512 authored Sep 30, 2024
2 parents 1bb6f1b + f2b6a9c commit 539356c
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 4 deletions.
10 changes: 9 additions & 1 deletion lib/dataloader/ecto.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down
2 changes: 1 addition & 1 deletion priv/test_repo/migrations/1_migrate_all.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Absinthe.Ecto.TestRepo.Migrations.MigrateAll do
defmodule Dataloader.Ecto.TestRepo.Migrations.MigrateAll do
use Ecto.Migration

def change do
Expand Down
29 changes: 28 additions & 1 deletion test/dataloader/ecto/belongs_to_test.exs
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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!()
Expand Down Expand Up @@ -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

Check failure on line 74 in test/dataloader/ecto/belongs_to_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.13 / OTP 24

test belongs_to is inside the embedded schema (Dataloader.Ecto.BelongsToTest)

Check failure on line 74 in test/dataloader/ecto/belongs_to_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.13 / OTP 25

test belongs_to is inside the embedded schema (Dataloader.Ecto.BelongsToTest)
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
9 changes: 9 additions & 0 deletions test/support/address.ex
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions test/support/country.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
defmodule Dataloader.Country do
use Ecto.Schema

schema "countries" do
field(:name, :string)
end
end

0 comments on commit 539356c

Please sign in to comment.