[8.x] Eager loading MorphTo relationship does not honor each models $keyType #34531
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.
When eager loading a morphTo relationship that could contain models using a $keyType of 'string' each query that is built is not making sure the bound parameter is cast to string due to the dictionary being built using each model's ID as an array key.
Table A has morphTo "owner" relationship.
Table B is a related model that has a $keyType of string. Even though some of its values can be "numeric" at times.
Entries in table A for B include id values of [1,2,3]
When building the whereIn query for the database if you are not casting to a string MySQL will not use the index for that query when the column type is a char requiring a full table scan of Table B.
I really want to write tests for this but am not smart enough figure out a refactor that would give me the raw query before it's executed in
getResultsByType($type)
or even mock the query builder to catch the whereIn() call and examine the parameters passed in.Thanks for your consideration.