Elixir (exto.QueryError)deps/exto/lib/exto/association.ex:399:field`App.Location.post_id`in`where`在查询的架构中不存在

Elixir (exto.QueryError)deps/exto/lib/exto/association.ex:399:field`App.Location.post_id`in`where`在查询的架构中不存在,elixir,phoenix-framework,Elixir,Phoenix Framework,我有两个模型:定位和发布 # Models schema "locations" do ... has_many :posts, App.Post timestamps() end schema "posts" do ... has_one :location, App.Location end 我能够成功地进行插入 changeset = location |> build_assoc(:posts) |> Post.changeset(para

我有两个模型:定位和发布

# Models
schema "locations" do
  ...
  has_many :posts, App.Post
  timestamps()
end

schema "posts" do
  ...
  has_one :location, App.Location
end
我能够成功地进行插入

changeset =
  location
  |> build_assoc(:posts)
  |> Post.changeset(params)
Repo.insert(changeset)
但是,当我尝试使用
Repo.preload
加载数据时,我得到一个错误

App.Repo.one(App.Post) |> App.Repo.preload(:location)
** (Ecto.QueryError) deps/ecto/lib/ecto/association.ex:399: field `App.Location.post_id` in `where` does not exist in the schema in query:

from l in App.Location,
  where: l.post_id == ^5,
  select: {l.post_id, l}

如果表中的另一个表有外键,则需要使用的关系是
属于
,而不是
有一个
。这应该起作用:

schema "posts" do
  ...
  belongs_to :location, App.Location
end

如果表中的另一个表有外键,则需要使用的关系是
属于
,而不是
有一个
。这应该起作用:

schema "posts" do
  ...
  belongs_to :location, App.Location
end

“你把关联添加到外星模式中了吗?”@Dogbert给我一分钟。Stackoverflow在我添加标签时提交了帖子,并按enter键…我在帖子迁移中有
add:location\u id,references(:locations,on\u delete::nothing)
。你在Ecto模式中添加了关联吗?@Dogbert给我一分钟。Stackoverflow在我添加标签并按enter键时提交了帖子…我在帖子迁移中有
add:location\u id,references(:locations,on\u delete::nothing)
。我想我应该检查一下我的数据库设计。谢谢你的帮助!我想我应该回顾一下我的数据库设计。谢谢你的帮助!