与以下混合部门:

[{:phoenix, "~> 0.15"},
 {:phoenix_ecto, "~> 1.0.0"},
 {:postgrex, ">= 0.0.0"},
 {:phoenix_html, "~> 2.1"},
 {:phoenix_live_reload, "~> 0.5", only: :dev},
 {:cowboy, "~> 1.0"}]


以及以下型号:

# foo.ex
has_many: :bars, App.Bar

# bar.ex
belongs_to: :foo, App.Foo


在数据库中已插入bars时,尝试预加载Foo时出现错误:

Repo.all(Foo) |> Repo.preload(:bars)


产量:

** (FunctionClauseError) no function clause matching in Postgrex.Extensions.Binary.encode/4
    (ecto) lib/ecto/repo/preloader.ex:49: Ecto.Repo.Preloader.do_preload/4


当尚未插入[]时,它不会产生错误(只是Foo)。

Bar迁移:

defmodule App.Repo.Migrations.CreateBar do
  use Ecto.Migration

  def change do
    create table(:bars) do
      add :title, :string
      add :foo_id

      timestamps
    end

    create index(:bars, [:foo_id])
  end
end

最佳答案

您在迁移中缺少:foo_id的类型。

请尝试以下操作:

defmodule App.Repo.Migrations.CreateBar do
  use Ecto.Migration

  def change do
    create table(:bars) do
      add :title, :string
      add :foo_id, references(:foos)

      timestamps
    end

    create index(:bars, [:foo_id])
  end
end


您可以在http://hexdocs.pm/ecto/0.15.0/Ecto.Migration.html阅读有关迁移的文档

关于elixir - phoenix_ecto:加载asoc时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31966119/

10-11 16:19