phoenix_ecto : 加载关联时出错
phoenix_ecto : error when loading assoc
与以下混合部门:
[{: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
阅读迁移文档
与以下混合部门:
[{: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
阅读迁移文档