Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Elixir 密钥:在中找不到用户id。长生不老药预加载问题_Elixir_Phoenix Framework - Fatal编程技术网

Elixir 密钥:在中找不到用户id。长生不老药预加载问题

Elixir 密钥:在中找不到用户id。长生不老药预加载问题,elixir,phoenix-framework,Elixir,Phoenix Framework,我试图在构建联盟结构时添加用户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<associa

我试图在构建联盟结构时添加用户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})
模型 联盟 user.ex
我需要使用回购预加载吗?我不确定它要我做什么?

如果仔细查看错误,它会说
:user\u id
找不到。事实上,它并不存在于打印的地图中,但是
:users\u id
确实存在。这只能意味着您有一个名为
users
的关系,而不是
user
,这正是问题所在<代码>属于:用户应该是
属于:用户

如果您遵循此链接, 您将看到它在exto.Schema=>Functions=>bellings\u to/3=>Options中提到

属于(名称、可查询、选项\\[])

:foreign_key-设置外键字段名称,默认为以_id为后缀的关联名称。例如,belish_to:company将定义:company_id的外键


在您的情况下,
属于:user
将定义
:user\u id

的外键不应该是
属于:user
?是的!啊!谢谢
  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
  schema "leagues" do
    field :name, :string
    has_many :teams, Statcasters.Teams.Team
    belongs_to :users, Statcasters.Coherence.User

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

    timestamps()
  end