Error upgrading to 1.15.x - associated module xyz is not an Ecto Schema

We are trying to upgrade to 1.15.x and running into this issue:

Compiling nnn files (.ex)
warning: invalid association `logs` in schema MyProject.Data.Alert: associated module MyProject.Data.AlertLog is not an Ecto schema
  lib/my_project/data/alert.ex:1: MyProject.Data.Alert (module)

Compilation failed due to warnings while using the --warnings-as-errors option

Though the associated module AlertLog is in fact an Ecto.Schema. Does this have to do with the order in which these files (Alert, AlertLog) appear in Code path?

I tried adding prune_code_paths: false in mix.exs and that worked. But the warning in the Changelog for Elixir v1.15 — Elixir v1.15.7 documentation about runtime bug is a big concern.

So, not sure how to resolve this compile time warning/error. Any help/suggestions would be appreciated.

1 Like

While upgrading our project, I also encountered similar warnings, but eventually, it turned out it was some typo or an invalid case.

So, if it’s okay, can you share the MyProject.Data.Alert schema and associated logs schema to check?

Thanks @masudme09. Here are the 2 schemas - didn’t see anything wrong with them though:

schema "alerts" do
    field :account_id, RawUUID
    field :assignee_id, RawUUID
    has_many :comments, Comment
    field :corrective_action, :string
    field :creation_method, Ecto.Enum, values: @creation_methods
    field :description, :string
    field :description_type, Ecto.Enum, values: @description_types
    field :event_id, RawUUID
    field :external_link, :string
    field :feed_provider_event_id, :string
    has_many :logs, AlertLog
    field :market_id, RawUUID
    field :order_id, RawUUID
    field :payment_id, RawUUID
    field :settlement_id, RawUUID
    field :severity, Ecto.Enum, values: @severities
    field :source, Ecto.Enum, values: @sources
    field :status, Ecto.Enum, values: @statuses
    field :title, :string
    field :trade_id, RawUUID
    field :type_id, RawUUID
    field :user_id, RawUUID
    field :inserted_at, RawTimestamp
    field :inserted_by, RawUUID
    field :updated_at, RawTimestamp
    field :updated_by, RawUUID
  end
schema "alert_logs" do
    field :alert_id, RawUUID
    field :account_id, RawUUID
    field :assignee_id, RawUUID
    field :description, :string
    field :event_id, RawUUID
    field :external_link, :string
    field :market_id, RawUUID
    field :order_id, RawUUID
    field :payment_id, RawUUID
    field :settlement_id, RawUUID
    field :severity, Ecto.Enum, values: @severities
    field :status, Ecto.Enum, values: @statuses
    field :title, :string
    field :trade_id, RawUUID
    field :user_id, RawUUID
    field :updated_at, RawTimestamp
    field :updated_by, RawUUID
  end

With the error, it looks like the MyProject.Data. AlertLog module is missing the following line.

use Ecto.Schema

Can you verify this?

Yes, the module is using the use Ecto.Schema line.

Just to let you know - all these modules compiled fine under Elixir 1.14.x - all we are doing is upgrading to 1.15.6.

Just because it’s mandatory to always ask, have you done the ol’ turn it off and on again? ie:

$ rm -rf _build deps && mix do deps.get, compile

I have copied your schema in one of my projects running with Elixir 1.15.7…and didn’t have any warnings. So, I firmly believe the problem is with something else.

You can try to shrink it by removing some file content to find the exact root cause of it.

Or maybe try to rewrite the schema and check when it starts giving the warnings and so on.

@sodapopcan Yes, I did this step and it works locally.

This failure consistently happens in our build pipeline. Not able to reproduce this error consistently in local machine (multiple developers tries this on their machines but no error).

1 Like

Did you try resetting caches for CI as well?

1 Like

Yes, reset the caches and also tried using the prune_code_paths: false in mix.exs as mentioned in the Changelog for v1.15. Still the same error on the pipeline.

Which CI? How are you configuring the caches?

We are using Bitbucket pipelines. It has a build cache and dependency cache. Even if I clear those caches, we are running into the same issue.

I have exactly same issue with GitHub CI. Only thing that helping is to include Code.ensure_compiled!/1 as a hack to make CI working.

@kxkannan have you found any solution?

Hello @Mikhail where do you include Code.ensure_compiled! - in the module where it errors (in this case the Alert.ex module)?

@kxkannan Yes, exactly, put it somewhere before schema macro.

Code.ensure_compiled!(AlertLog)

But this is just a workaround to avoid warnings. I can’t understand what is root cause of itl.

This looks similar to what you are saying Intermittent "invalid association" warnings at compile time · Issue #4293 · elixir-ecto/ecto · GitHub

1 Like

Thanks for the link @joey_the_snake - it’s the same issue that we are running into. Looks like this is fixed in 1.16 - we may have to wait for that release for the upgrade.