fix: fix required relationships and add test

This commit is contained in:
Zach Daniel 2021-03-28 13:43:58 -04:00
parent e65d47d386
commit 11d658d077
2 changed files with 14 additions and 2 deletions

View file

@ -797,13 +797,13 @@ defmodule Ash.Changeset do
# Attributes that are private and/or are the source field of a belongs_to relationship # Attributes that are private and/or are the source field of a belongs_to relationship
# are typically not set by input, so they aren't required until the actual action # are typically not set by input, so they aren't required until the actual action
# is run. # is run.
defp attributes_to_require(resource, _private_and_belongs_to? = true) do defp attributes_to_require(resource, true = _private_and_belongs_to?) do
resource resource
|> Ash.Resource.Info.attributes() |> Ash.Resource.Info.attributes()
|> Enum.reject(&(&1.allow_nil? || &1.generated?)) |> Enum.reject(&(&1.allow_nil? || &1.generated?))
end end
defp attributes_to_require(resource, _private_and_belongs_to? = false) do defp attributes_to_require(resource, false = _private_and_belongs_to?) do
belongs_to = belongs_to =
resource resource
|> Ash.Resource.Info.relationships() |> Ash.Resource.Info.relationships()

View file

@ -581,6 +581,18 @@ defmodule Ash.Test.Actions.CreateTest do
|> Api.create!() |> Api.create!()
end end
end end
test "allows creating with the required belongs_to relationship" do
author =
Author
|> Ash.Changeset.for_create(:create, bio: "best dude")
|> Api.create!()
ProfileWithBelongsTo
|> Ash.Changeset.for_create(:create)
|> Ash.Changeset.replace_relationship(:author, author)
|> Api.create!()
end
end end
describe "list type" do describe "list type" do