Fix multi-database polymorphic preloading with equivalent table names #50382
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation / Background
I work with an application that utilizes a polymorphic association that spans multiple databases. A basic example would be something like this, where
Notification#notifiable
could referenceComment
orOtherNamespace::Comment
, which exist on two separate databases but happen to have the same table name:Prior to upgrading to Rails 7.0, the following would preload both
Comment
s andOtherNamespace::Comment
s:After upgrading to Rails 7.0, we noticed that only
Comment
s were being preloading andOtherNamespace::Comment
s were not.After some digging, I tracked the difference in behavior down to PR #41385. In this PR there were some optimizations made to combine association preloading into less queries when possible. This was later optimized a bit further in PR #41597.
From what I can tell
ActiveRecord::Associations::Preloader::Batch#group_and_load_similar
, queries are being grouped and loaded. That comparison is being done based onActiveRecord::Associations::Preloader::Association::LoaderQuery#eql?
, which is the following:As you can see, in the polymorphic use case shown above the loaders for
OtherNamespace::Comment
andComment
would be considered equivalent since they have the sameassociation_key_name
(notifiable
in this case),table_name
(comments
in this case), andvalues_for_queries
(id
in this case). By comparing them this way it results inOtherNamespace::Comment
not being preloaded after the Rails 7.0 upgrade.Detail
Using the existing models and relationships in the test suite, I was able to generate a failing test case. I've also included a proposed fix which adds a loader comparison on
scope.connection_specification_name
. The idea is to prevent two tables with the same name on two separate databases from being considered equivalent when preloading associations.Another idea would be to replace the
table_name
comparison withname
(the class name). I tried this and all tests pass, but I'm unsure if this could be problematic in other cases, such as when using single table inheritance. If there's another way of achieving the desired result with a different comparison, I'm happy to change the logic!Checklist
Before submitting the PR make sure the following are checked:
[Fix #issue-number]
Happy to add a Changelog entry if it's needed.