Elixir phoenix_-Exto:加载assoc时出错
使用以下混合DEP:Elixir phoenix_-Exto:加载assoc时出错,elixir,phoenix-framework,ecto,Elixir,Phoenix Framework,Ecto,使用以下混合DEP: [{: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
[{: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
当数据库中已插入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\u 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
您可以在上阅读迁移文档,我已经在代码中指定了它,我的缺点是没有将它放在这里,只是想描述关系,并进行相应编辑。您也可以发布迁移吗?当外键不正确时可能会发生此错误。随着迁移而更新。我认为您需要为foo_id字段指定类型(整数)。