查询中的模型源中不存在“join”中的Phoenix和Ecto字段x

时间:2015-10-17 10:43:32

标签: elixir phoenix-framework ecto

燮, 我正在通过我的项目工作,我遇到了奇怪的问题。我试图通过以下方案加入PostgreSQL中的表:

defmodule Hangman.MasterCat do
  use Hangman.Web, :model

  schema "mastercats" do
    field :name, :string

    has_many :categories, Hangman.Category
  end

  ...

end

第二个:

defmodule Hangman.Category do
  use Hangman.Web, :model

  schema "categories" do
    field :name, :string

    has_many :words, Hangman.Word
    belongs_to :mastercats, Hangman.MasterCat
  end

  ...

end

和行动代码:

def index(conn, _params) do
    query = from p in MasterCat,
      join: c in Category,
      on: c.mastercat_id == p.id,
      select: {p, c}

    a = Repo.all(query)
    render conn, "index.json", mastercats: a
end

字段“mastercat_id”在迁移文件中定义为对类别的引用:

defmodule Hangman.Repo.Migrations.CreateCategories do
  use Ecto.Migration

  def change do
    create table(:categories) do
      add :name, :string
      add :mastercat_id, references(:mastercats)

      timestamps
    end
  end
end

并且Action返回以下错误:

web/controllers/mastercat_controller.ex:8: field `Hangman.Category.mastercat_id` in `join` does not exist in the model source in query:

from m in Hangman.MasterCat,
  join: c in Hangman.Category,
  on: c.mastercat_id == m.id,
  select: {m, c}

我不仅仅感谢任何帮助。 干杯, Haito

https://github.com/Hajto/hangmanelixir

0 个答案:

没有答案