Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.Sign up
Ecto preload function fails for many_to_many and has_many through #2534
This is for the preload function in the form like:
From a discussion at: https://elixirforum.com/t/ecto-preload-function-discarding-data/14031/5
As far as I can tell:
has_many through passes a list of child ids to the preload function and will exclude anything returned that doesn’t have an id that matches one of those ids. (Only allows a use case where you want the exact same items that would be returned from the main query). This is unexpected because it’s a different set of ids compared to the other cases. I would expect that instead it would be passing the parent ids and letting the query figure out how to load the data.
many_to_many passes the parent ids to the preload function and will exclude anything returned that doesn’t have an id that matches those ids. (Only would work accidentally if the parent and child had the same ids). I would expect the matching of children should be the referenced parent_id in the child and not the child's own id.
has_many passes the parent ids to the preload function and will work as expected matching the parent id to the “parent_id” property on the child items. (Works as expected)
I implemented a simple test app: https://github.com/geofflane/ecto_preload_test