-
Notifications
You must be signed in to change notification settings - Fork 21.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unexpected Type Casting in ActiveRecord::Calculations#pluck #28044
Comments
ActiveRecord::Calculations#pluck
It seems to be happening because line 87 changes the type of the Comments column from I don't see any examples of |
This actually comes both from adapter and type casting inside of pluck method. Adapter uses column names(which are the same in this case) for type definition. Type overrides for To work this around you can provide different names for identical named columns like this: |
…ast. See rails/rails#28044 for details.
This issue has been automatically marked as stale because it has not been commented on for at least three months. |
This is still a problem in master. Below is a reproduction:
|
This is a HUGE issue as it silently cast UUID and create a collision issue, mixing records. |
@why-el This is a nice repro, thanks! I guess this line:
should read:
|
A very similar reproduction that silently corrupts data. When a model has an integer ID field, it can still have a I'm thinking Rails could throw an exception when you set up the
|
Fixed by #39264. |
Steps to reproduce
Post
with integer-based primary keys.Comment
with UUID-based primary keys.Post
should have manyComment
s and aComment
should belong to aPost
.Post
andComment
IDs usingPost.joins(:comments).pluck('posts.id', 'comments.id')
.See the following code for an example and test case: https://gist.github.com/awmichel/ba34381fe418f761932b12ef8ff2063f
Expected behavior
The expected result is an array of arrays containing pairs of integer and UUID primary keys belonging to
Post
andComment
records.Ex:
[[1, "316d1975-6126-4475-bf8b-9c32ce4ee7a6"]]
Actual behavior
The actual behavior is an array of arrays containing pairs of integer and truncated UUID primary keys belonging to
Post
andComment
records.Ex:
[[1, 316]]
System configuration
Rails version: 5.0.1
Ruby version: 2.4.0
Additional Information
I came across this bug while debugging an exception thrown in cerebris/jsonapi-resources. The exception was
undefined method `_type' for NilClass:Class
. After spending some time debugging, I was able to trace the exception to this line which attempts to pluck model IDs and timestamps of the records it returns.So far I've been able to track the bug down to this line. The
result
just before that line contains the full UUID of the related records. After callingcast_values
the UUID is truncated down to the first X digits in the UUID.The text was updated successfully, but these errors were encountered: