key:找不到user_id.Elixir Preload问题

Bit*_*ise 2 elixir phoenix-framework

我正在尝试在构建联盟结构时添加user_id,但它给了我这个错误:

** (KeyError) key :user_id not found in: %{__meta__: #Ecto.Schema.Metadata<:built, "leagues">, __struct__: Statcasters.Schema.League, id: nil, inserted_at: nil, name: nil, teams: #Ecto.Association.NotLoaded<association :teams is not loaded>, updated_at: nil, users: #Ecto.Association.NotLoaded<association :users is not loaded>, users_id: nil}
(stdlib) :maps.update(:user_id, {{:., [line: 14], [{:user, [line: 14], nil}, :id]}, [line: 14], []}, %{__meta__: #Ecto.Schema.Metadata<:built, "leagues">, __struct__: Statcasters.Schema.League, id: nil, inserted_at: nil, name: nil, teams: #Ecto.Association.NotLoaded<association :teams is not loaded>, updated_at: nil, users: #Ecto.Association.NotLoaded<association :users is not loaded>, users_id: nil})
Run Code Online (Sandbox Code Playgroud)

这是我的控制器:

联盟控制器

  def new(conn, _params) do
    user = Repo.get!(User, conn.assigns.current_user.id)
    changeset = %League{user_id: user.id}
      |> League.changeset()

    render(conn, "new.html", changeset: changeset)
  end
Run Code Online (Sandbox Code Playgroud)

楷模

league.ex
  schema "leagues" do
    field :name, :string
    has_many :teams, Statcasters.Teams.Team
    belongs_to :users, Statcasters.Coherence.User

    timestamps()
  end
Run Code Online (Sandbox Code Playgroud) user.ex
  schema "users" do
    field :name, :string
    field :email, :string
    coherence_schema()
    has_many :leagues, Statcasters.Schema.League

    timestamps()
  end
Run Code Online (Sandbox Code Playgroud)

我需要使用Repo.preload吗?我不确定它告诉我做什么?

Dog*_*ert 6

如果仔细观察错误,就说:user_id没找到.实际上,它在印刷的地图中:users_id不存在,但确实存在.这只能意味着你有一个名为users而不是的关系user,这正是问题所在.belongs_to :users应该是belongs_to :user.