0

Sup,我正在完成我的项目,但遇到了奇怪的问题。我试图通过以下方案加入 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}

我将非常感谢任何帮助。干杯,海托

https://github.com/Hajto/hangmanelixir

4

0 回答 0