Mr H
Mr H

Reputation: 5304

(RuntimeError) casting assocs with cast/4 is not supported, use cast_assoc/3 instead

I don't understand this error and I searched a lot. Non of the examples are showing multiple association? Here are the codes.

My AccessList:

defmodule Db.AccessList do
    use Ecto.Schema

    schema "access_lists" do
        belongs_to :user_id, Db.User
        belongs_to :role_id, Db.Role
        belongs_to :asset_id, Db.Asset
        belongs_to :project_id, Db.Project

        timestamps()
    end

    def changeset(model, params \\ %{}) do
            model
            |> Ecto.Changeset.cast(params, [:user_id, :role_id, :asset_id, :project_id])
            |> Ecto.Changeset.validate_required([:user_id, :role_id])
    end
end

test:

defmodule AccessListTest do
    alias Db.{ AccessList }
    use ExUnit.Case

    @valid_attr %{user_id: 1, role_id: 1, asset_id: 1, project_id: 1 }

    @tag :wip
    test "ACCESSLIST.1 valid attribute" do
        changeset = AccessList.changeset(%AccessList{}, @valid_attr)
        assert changeset.valid?
    end

    @tag :wip
    test "ACCESSLIST.2 invalid attribute" do
        invalid_attr = Map.put(@valid_attr, :user_id, nil)
        changeset = AccessList.changeset(
            %AccessList{}, invalid_attr
        )
        refute changeset.valid?
    end
end

Here is the error:

......

  1) test ACCESSLIST.1 valid attribute (AccessListTest)
     test/access_list_test.exs:8
     ** (RuntimeError) casting assocs with cast/4 is not supported, use cast_assoc/3 instead
     code: changeset = AccessList.changeset(%AccessList{}, @valid_attr)
     stacktrace:
       (ecto) lib/ecto/changeset.ex:485: Ecto.Changeset.type!/2
       (ecto) lib/ecto/changeset.ex:464: Ecto.Changeset.process_param/7
       (elixir) lib/enum.ex:1826: Enum."-reduce/3-lists^foldl/2-0-"/3
       (ecto) lib/ecto/changeset.ex:449: Ecto.Changeset.cast/6
       (db) lib/db/access_list.ex:15: Db.AccessList.changeset/2
       test/access_list_test.exs:10: (test)



  2) test ACCESSLIST.2 invalid attribute (AccessListTest)
     test/access_list_test.exs:15
     ** (RuntimeError) casting assocs with cast/4 is not supported, use cast_assoc/3 instead
     code: changeset = AccessList.changeset(
     stacktrace:
       (ecto) lib/ecto/changeset.ex:485: Ecto.Changeset.type!/2
       (ecto) lib/ecto/changeset.ex:464: Ecto.Changeset.process_param/7
       (elixir) lib/enum.ex:1826: Enum."-reduce/3-lists^foldl/2-0-"/3
       (ecto) lib/ecto/changeset.ex:449: Ecto.Changeset.cast/6
       (db) lib/db/access_list.ex:15: Db.AccessList.changeset/2
       test/access_list_test.exs:18: (test)



Finished in 0.3 seconds
53 tests, 2 failures, 45 skipped

Upvotes: 3

Views: 3331

Answers (1)

Dogbert
Dogbert

Reputation: 222408

Since you have _id (e.g. user_id) at the end of the belongs_to name, Ecto thinks the association is called user_id and the database field is user_id_id. That is why the error message says user_id is an association and wants you to use cast_assoc. You need to remove the _id suffix from all association names as I'm assuming the database fields have only one _id at the end:

belongs_to :user, Db.User
belongs_to :role, Db.Role
belongs_to :asset, Db.Asset
belongs_to :project, Db.Project

Upvotes: 7

Related Questions