ecto_fixtures
ecto_fixtures copied to clipboard
Problem with `embeds_many`
Given the following schemas:
defmodule MyApp.Games.Game do
use Ecto.Schema
import Ecto.Changeset
schema "games" do
field :letters, :string
embeds_many :game_words, MyApp.Games.GameWord
timestamps()
end
@doc false
def changeset(game, attrs) do
game
|> cast(attrs, [:letters])
|> cast_embed(:game_words, required: true)
|> validate_required([:letters])
end
end
defmodule MyApp.Games.GameWord do
use Ecto.Schema
import Ecto.Changeset
embedded_schema do
field :word_id, :integer
field :name, :string
field :score, :integer
end
@doc false
def changeset(game_word, attrs) do
game_word
|> cast(attrs, [:word_id, :name, :score])
|> validate_required([:word_id, :name, :score])
end
end
I would assume that I could just create the following fixture:
games model: MyApp.Games.Game, repo: MyApp.Repo do
game1 do
letters "ABCDEFGHIJKLMNOPQRSTUVWXY"
game_words [
%{
name: "FOO",
score: 4,
word_id: 31327
},
%{
name: "BAR",
score: 5,
word_id: 32012
},
%{
name: "BAZ",
score: 8,
word_id: 32196
}
]
end
end
But when trying to use that fixture, I get the following error:
** (Ecto.InvalidChangesetError) could not perform insert because changeset is invalid.
Applied changes
%{
id: 579810422,
letters: "ABCDEFGHIJKLMNOPQRSTUVWXY"
}
Params
nil
Errors
%{game_words: ["is invalid"]}
Changeset
#Ecto.Changeset<
action: :insert,
changes: %{
id: 579810422,
letters: "ABCDEFGHIJKLMNOPQRSTUVWXY"
},
errors: [game_words: "is invalid"],
data: #MyApp.Games.Game<>,
valid?: false
>
stacktrace:
(ecto) lib/ecto/repo/schema.ex:128: Ecto.Repo.Schema.insert!/4
(ecto_fixtures) lib/ecto/fixtures/insertion.ex:6: anonymous fn/4 in EctoFixtures.Insertion.process/2
(elixir) lib/enum.ex:1899: Enum."-reduce/3-lists^foldl/2-0-"/3
test/my_app_web/controllers/admin/game_controller_test.exs:3: anonymous fn/2 in MyAppWeb.GameControllerTest.__ex_unit_setup_1/1
(elixir) lib/enum.ex:1899: Enum."-reduce/3-lists^foldl/2-0-"/3
test/my_app_web/controllers/admin/game_controller_test.exs:3: MyAppWeb.GameControllerTest.__ex_unit_setup_1/1
test/my_app_web/controllers/admin/game_controller_test.exs:1: MyAppWeb.GameControllerTest.__ex_unit__/2
Is this library even supposed to support embedded schemas?