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
Querying against a non-default primary key uses wrong value #18813
Comments
I don't know if it helps, but I noticed this in |
This looks like the correct behavior. You are confusing the primary_key and foreign_key options. |
Never mind, I misread your issue. |
This is my daily reminder not to go through the issue tracker before I've had my coffee... |
@sgrif In production code, it uses a named |
Yes, you have it right. I was just reading it wrong. I'm drinking my coffee now, and then I'll fix it. 😁 |
This might not be something that can be backported to pre-5.0, since the fix on master depends a lot on the refactoring work I've been doing this year. I'll see what I can do to get it on 4.2 and 4.1 though |
In case I hit this again: The obvious work-around is to query the foreign-key column with the correct primary-key value instead of the object, e.g. in the example above
returns the correct result. |
You can also just pass the ID without changing the column. Doing |
While we query the proper columns, we go through normal handling for converting the value to a primitive which assumes it should use the table's primary key. If the association specifies a different value (and we know that we're working with an association), we should use the custom primary key instead. Fixes #18813. Conflicts: activerecord/lib/active_record/relation/predicate_builder/association_query_handler.rb activerecord/lib/active_record/table_metadata.rb
While we query the proper columns, we go through normal handling for converting the value to a primitive which assumes it should use the table's primary key. If the association specifies a different value (and we know that we're working with an association), we should use the custom primary key instead. Fixes #18813. Conflicts: activerecord/lib/active_record/relation/predicate_builder/association_query_handler.rb activerecord/lib/active_record/table_metadata.rb
@sgrif Thanks for the quick fix! |
Any time. It's my job. :) |
I have a gist: https://gist.github.com/x2764tech/4ff7c015cd5d7137c295
I have the follow models:
and the test
The data is created successfully, but the query to check whether a post exists uses
Post.id
and notPost.system_id
:The query should use the primary key specified in the relationship.
The text was updated successfully, but these errors were encountered: