coryodaniel / arbor

Ecto elixir adjacency list and tree traversal. Supports Ecto versions 2 and 3.
MIT License
239 stars 26 forks source link

prefix not found in struct_fields[:__meta__].source #25

Closed chungwong closed 5 years ago

chungwong commented 5 years ago

I am using {:arbor, "~> 1.0.6"} with table

create table(:comments) do
   add(:body, :text)
   add(:parent_id, references(:comments), on_delete: :delete_all)
end
create index(:comments, [:parent_id])

and a schema

defmodule Test.Comment do
  use Ecto.Schema

  use Arbor.Tree,
    foreign_key: :parent_id,
    foreign_key_type: :integer

  import Ecto.Query

  schema "comments" do
    field(:body, :string)
    belongs_to(:parent, Test.Comment)
  end
end

And I am getting this error

== Compilation error in file lib/test/comment.ex ==
** (MatchError) no match of right hand side value: "comments"
    expanding macro: Arbor.Tree.__before_compile__/1
    lib/test/comment.ex:1: Testpu.Comment (module)
    (elixir) lib/kernel/parallel_compiler.ex:198: anonymous fn/4 in Kernel.ParallelCompiler.spawn_workers/6

It turns out this line https://github.com/coryodaniel/arbor/blob/1c63aaef2d194742ff834941aa00a376b698e398/lib/arbor/tree.ex#L40 causing problem.

It is expecting a prefix in the tuple {prefix, source} while struct_fields[:__meta__].source only returns {source} which resulted in a MatchError.

It looks like it has been fixed for Ecto3, is support for Ecto2 still maintained?

chungwong commented 5 years ago

I cannot believe my eyes. I am actually using Ecto 3 and I should be using {:arbor, "~> 1.1.0"}.