Error Preloading Join Table Referencing Another Join Table

I’m having trouble preloading a relationship from a join table through another join table because Ecto seems to be adding another underscore to one of the fields causing preloads to fail. For some reason the id key of the first join table has an additional underscore added to it which ends up querying users__todos_id(note the double underscore after user) instead of user_todos_id which causes preloading to fail. In this instance, instead of users_todos_id from Users_Todos_Tags it seems to be querying users__todos_id(note the extra underscore) wherein it consequently crashes and is unable to find the id.

Can anyone point out what I’m doing wrong or any assumptions Ecto is making? I don’t see a double underscore anywhere in my relationships so I have no idea why the model is failing to load/crashing the query. Does the issue arise because I have a single underscore in the schema name(join table users_todos) already and that somehow conflicts with an implicit relationship?

If it’s any help at all I searched through my entire project(project wide ctrl-f) and the only place users__todos comes up (with the double underscore) is the MyApp.Users_TodosControllerTest which was generated at some point earlier in the project by mix.

Elixir 1.3.4
Ecto 2.0.5
OSX El Capitan
(PostgreSQL) 9.5.5

Attached is the gist of the models/relationships

At first glance the defmodule MyApp.Users_Todos might be something to do with it. Have you tried just MyApp.UsersTodos? (Same for the other module names)

So I just switched over the module names like you said to not contain the underscore and it all seems to work now…Such a strange error so I guess there’s something going on under the hood in Ecto? In any case thank you

I would assume that Ecto is inflecting the foreign key field name from the module name, and by default this would be e.g. UsersTodos => users_todos_id. Your extra _ in the module name meant it looked for users__todos_id

1 Like