fuelen / ecto_erd

A mix task for generating Entity Relationship Diagram from Ecto schemas available in your project.
Apache License 2.0
215 stars 15 forks source link

task crashes when run #8

Closed tadasajon closed 2 years ago

tadasajon commented 2 years ago

I just installed this library and tried to run mix ecto.gen.erd. It immediately crashed with this error:

** (UndefinedFunctionError) function SpecialUser.__schema__/1 is undefined (module SpecialUser is not available)
    SpecialUser.__schema__(:source)
    (ecto_erd 0.4.0) lib/ecto/erd/graph.ex:134: Ecto.ERD.Graph.from_relation_struct/1
    (elixir 1.13.0-rc.0) lib/enum.ex:4076: Enum.flat_map_list/2
    (ecto_erd 0.4.0) lib/ecto/erd/graph.ex:79: Ecto.ERD.Graph.components/2
    (elixir 1.13.0-rc.0) lib/enum.ex:4076: Enum.flat_map_list/2
    (elixir 1.13.0-rc.0) lib/enum.ex:4077: Enum.flat_map_list/2
    (ecto_erd 0.4.0) lib/ecto/erd/graph.ex:9: Ecto.ERD.Graph.new/2
    (ecto_erd 0.4.0) lib/mix/tasks/ecto.gen.erd.ex:138: Mix.Tasks.Ecto.Gen.Erd.run/

I am able to create SpecialUsers in my app when I am using it and my app works fine in this regard, so I am not sure why this library is complaining.

fuelen commented 2 years ago

@tadasajon have you tried removing _build? The library simply uses all modules which have __schema__/1 function https://github.com/fuelen/ecto_erd/blob/1dd8b9a4f85dd10f435f3cbf779cd6f95b375b5e/lib/ecto/erd/schema_modules.ex#L4-L16

tadasajon commented 2 years ago

Thanks much for responding.

I have a file in my codebase that looks like this:

defmodule Quizdrill.Accounts.SpecialUser do
  use Ecto.Schema
  import Ecto.Changeset

  schema "special_users" do
    field :special_id, :integer
    field :first_name, :string
    field :last_name, :string
    field :username, :string

    timestamps()
  end

  @doc false
  def changeset(special_user, attrs) do
    special_user
    |> cast(attrs, [:special_id, :first_name, :last_name, :username])
    |> validate_required([:special_id])
    |> unique_constraint(:special_id)
  end
end

I am assume that because I have the line use Ecto.Schema that there will be a __schema__/1 function. Am I incorrect?

fuelen commented 2 years ago

Yes, use Ecto.Schema defines __schema__/1 function.

Check your other modules, which have a line like belongs_to :special_user, SpecialUser. Seems like one of those modules references to the module which doesn't exist. Perhaps you don't have declared alias for Quizdrill.Accounts.SpecialUser in one of those modules.

fuelen commented 2 years ago

Hey @tadasajon Any luck?

jjl commented 2 years ago

They seem to have forgotten this issue, but fortunately I was able to replicate. I locally patched my copy to give debugging output (IO.inspect) and was able to get it to run by commenting out things that didn't refer to schemas.

However, the reason some of ours did this was because in bonfire, we have a pluggability thing going on and it's convenient for us to be able to list relationships that might not exist and just not use them if they don't.

So I wrapped a few function bodies in if function_exported?(...) with a Logger.warn branch when the module was not a schema or could not be found, offering a little debug information to help the user in case they did actually want to do something about them.

Unfortunately I did it on the copy in deps because i'd already hacked it with debugging output before, so I figured I'd ask before replicating the work to a git clone: is this something you'd consider accepting a PR for?

fuelen commented 2 years ago

@jjl Hello Yes, PR is welcome :)

fuelen commented 2 years ago

Resolved in #13