gpt4 book ai didi

elixir - key :user_id not found in. Elixir 预加载问题

转载 作者:行者123 更新时间:2023-12-01 23:08:07 25 4
gpt4 key购买 nike

我在构建联盟结构时尝试添加 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})

这是我的 Controller :

联盟管理员

  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

模型

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

timestamps()
end
用户.ex
  schema "users" do
field :name, :string
field :email, :string
coherence_schema()
has_many :leagues, Statcasters.Schema.League

timestamps()
end

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

最佳答案

如果您仔细查看错误,它会显示 :user_id not found。实际上它不存在于打印的 map 中,但 :users_id 确实存在。这只能意味着您有一个名为 users 而不是 user 的关系,这正是问题所在。 belongs_to :users 应该是 belongs_to :user

关于elixir - key :user_id not found in. Elixir 预加载问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47426250/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com