This is working code from an old codebase. I recently upgraded to version 1.10 of Elixir, and it’s no longer working, seeing this error:
defmodule MyApp.Accounts.AccountRole do
use MyApp.Schema
schema "account_roles" do
belongs_to(:account, Account)
belongs_to(:role, Role)
end
# No longer works. See error below
#@required_attributes ~w(role_id, account_id)a
# This works as expected.
@required_attributes [:role_id, :account_id]
def changeset(schema, params \\ %{}) do
schema
|> cast(params, @required_attributes)
end
def create_changeset(schema, params \\ %{}) do
schema
|> changeset(params)
|> validate_required(@required_attributes)
|> cast_assoc(:role)
|> cast_assoc(:account)
end
end
Here’s the error:
iex(13)> MyApp.Accounts.AccountRole.changeset(%MyApp.Accounts.AccountRole{}, %{account_id: "10d2bcf8-123123123"})
** (ArgumentError) unknown field `:"role_id,"` given to cast. Either the field does not exist or it is a :through association (which are read-only). The known fields are: :account, :account_id, :id, :role, :role_id
(ecto 3.3.1) lib/ecto/changeset.ex:549: Ecto.Changeset.type!/2
(ecto 3.3.1) lib/ecto/changeset.ex:518: Ecto.Changeset.process_param/7
(elixir 1.10.0-rc.0) lib/enum.ex:2102: Enum."-reduce/3-lists^foldl/2-0-"/3
(ecto 3.3.1) lib/ecto/changeset.ex:503: Ecto.Changeset.cast/6
Is this a bug or something I’m missing on my end?