Invalid association... associated schema does not exist

I have two schemas Todo and TodoAssignment. TodoAssignment keeps track of a log of users a given todo has been assigned to.

I’m getting a warning on my TodoAssignment module stating:

invalid association todo in schema Delega.TodoAssignment: associated schema Delega.Todo does not exist

The schema definitely exists and all the code seems to work fine. I’m seeing this on two tables that reference my Todo schema.

Todo Schema

defmodule Delega.Todo do
  use Ecto.Schema
  import Ecto.Changeset

  alias Delega.{Todo, Repo, TodoAssignment}

  @primary_key {:todo_id, :id, autogenerate: true}
  schema "todo" do
    belongs_to :team, Delega.Team, references: :team_id, type: :string

    has_many :channels, Delega.TodoChannel, foreign_key: :todo_id

    field :created_user_id, :string

    has_one :created_user, Delega.User, references: :created_user_id, foreign_key: :user_id
    has_many :assignments, Delega.TodoAssignment, foreign_key: :todo_id

    field :todo, :string
    field :status, :string
  end
end

TodoAssignment Schema

defmodule Delega.TodoAssignment do
  use Ecto.Schema

  alias Delega.Todo

  @primary_key {:todo_assignment_id, :id, autogenerate: true}
  schema "todo_assignment" do
    field :assigned_to_user_id, :string
    field :assigned_by_user_id, :string

    belongs_to :todo, Todo, references: :todo_id, foreign_key: :todo_id

    has_one :assigned_to_user, Delega.User,
      references: :assigned_to_user_id,
      foreign_key: :user_id

    has_one :assigned_by_user, Delega.User,
      references: :assigned_by_user_id,
      foreign_key: :user_id

    field :created_at, :utc_datetime_usec
    field :updated_at, :utc_datetime_usec
  end
end

It seems like regardless of whether I use references or foreign_key I get this warning. I also get it on my TodoChannel table.

Why am I getting these warnings?

First: welcome to the forum :slight_smile:

So, I tried the code you posted and, after removing the references to schemas not included (Delega.User, etc.), it compiled for me without error in a single app … but if I split them across apps in an umbrella application, then I could trigger the warning … sometimes. It appears that depending on the order of compilation and how the compiler resolves dependencies between the apps in the umbrella, it can end up in a situation where the Todo schema is not yet built / available and so it warns that it can not find that module. But it does compile, and I expect that at runtime with all the modules compiled, it all links and works just fine when being used.

Are you using an umbrella app?

2 Likes

It is not an umbrella app. In fact the files are in the same folder. Very strange!

When I try to create a release on an Ubuntu system I get the compiler error below. How can I setup the database associations without having compile time references between the files?

== Compilation error in file lib/delega/todo_assignment.ex ==
** (CompileError)  deadlocked waiting on module Delega.Todo
    (elixir) lib/code.ex:1026: Code.ensure_compiled/1
    (elixir) lib/code.ex:1046: Code.ensure_compiled?/1
    lib/ecto/association.ex:827: Ecto.Association.BelongsTo.after_compile_validation/2
    lib/ecto/schema.ex:1887: anonymous fn/4 in Ecto.Schema.__after_compile__/2
    (elixir) lib/enum.ex:1925: Enum."-reduce/3-lists^foldl/2-0-"/3
    lib/ecto/schema.ex:1884: Ecto.Schema.__after_compile__/2

== Compilation error in file lib/delega/todo_channel.ex ==
** (CompileError)  deadlocked waiting on module Delega.Todo
    (elixir) lib/code.ex:1026: Code.ensure_compiled/1
    (elixir) lib/code.ex:1046: Code.ensure_compiled?/1
    lib/ecto/association.ex:827: Ecto.Association.BelongsTo.after_compile_validation/2
    lib/ecto/schema.ex:1887: anonymous fn/4 in Ecto.Schema.__after_compile__/2
    (elixir) lib/enum.ex:1925: Enum."-reduce/3-lists^foldl/2-0-"/3
    lib/ecto/schema.ex:1884: Ecto.Schema.__after_compile__/2

== Compilation error in file lib/delega/todo.ex ==
** (CompileError)  deadlocked waiting on module Delega.TodoAssignment
    lib/delega/todo.ex:42: (module)
    (stdlib) erl_eval.erl:680: :erl_eval.do_apply/6

Compilation failed because of a deadlock between files.
The following files depended on the following modules:

  lib/delega/todo_assignment.ex => Delega.Todo
     lib/delega/todo_channel.ex => Delega.Todo
             lib/delega/todo.ex => Delega.TodoAssignment

Ensure there are no compile-time dependencies between those files and that the modules they reference exist and are correctly named

@negcx Able to solve the warning? I am getting the same on Elixir 1.9.4 (compiled with Erlang/OTP 22) exactly in the same schema situation you mentioned above.

@furqanaziz No, I have never solved it. The repo is now public: https://github.com/negcx/delega

It doesn’t seem to actually matter but the warning continues.

I wonder if the module-attribute with an Ecto.Query struct in it is part of the issue:

2 Likes