elixir-ecto / ecto

A toolkit for data mapping and language integrated query.
https://hexdocs.pm/ecto
Apache License 2.0
6.15k stars 1.43k forks source link

Associated module is not an Ecto schema #4371

Closed paulo-silva closed 5 months ago

paulo-silva commented 7 months ago

Elixir version

1.16.0

Database and Version

PostgreSQL 15.2

Ecto Versions

3.11.1

Database Adapter and Versions (postgrex, myxql, etc)

postgrex 0.17.4

Current behavior

While bumping elixir on my project, I noticed while compiling a warning like:

warning: invalid association `selection_master` in schema App.Schemas.Schema: associated module App.Schemas.AssocSchema is not an Ecto schema

Checking this previous issue: https://github.com/elixir-ecto/ecto/issues/4293 - elixir 1.16 was meant to fix this problem, but some devs are still experiencing this problem.

Expected behavior

This warn should not happen, like it wasn't when using previous versions of elixir.

josevalim commented 7 months ago

Hi @paulo-silva. We need a mechanism to reproduce the warning, otherwise there isn't much we can do about it. :(

paulo-silva commented 7 months ago

Hi @paulo-silva. We need a mechanism to reproduce the warning, otherwise there isn't much we can do about it. :(

@josevalim that makes sense, I'll try to reproduce it in a project example

paulo-silva commented 7 months ago

@josevalim I tried to reproduce that in a project example, but I'm unable to; I guess the issue is related to the project size. Is there any chance we can huddle and I can show you the issue in the real project?

I tried the strategy proposed in slack ecto channel as a temp workaround, but I started getting deadlocks

nulian commented 7 months ago

After some puzzling with this same error for me it was caused by having a module attribute reference to a different schema. Though most of the times doing that doesn't seem to cause any issues but in 1 case it caused the same error for me. Giving a warning everytime trying to build it.

@organization_role_id_admin OrganizationRole.admin_role_id()

Schema gets warning from has_many in organizations -> organization_memberships while this memberships references a third schema with the module attribute.

I guess because it waits for the belongs_to to be ready and the organization waits for the has_many to be ready causing the issue.

nulian commented 7 months ago

I saw another difference in our project from before the refactor that the has_many was removed from organization_role -> organization_memberships

When I readd the has_many it will solve the warning.

I guess because it is referenced in the has_many it knows to wait for it or something and no longer give the compile warning.

JoeriDijkstra commented 6 months ago

After changing @organization_role_id_admin OrganizationRole.admin_role_id() to a static value it seems to work great: @organization_role_id_admin "foobar"

so probably some race condition in the compiler like nulian said.

josevalim commented 5 months ago

Closing it as #4293 was reopened.