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
ActiveRecord does not cast the value passed in Hash #35205
Comments
Where does "foo" should be casted, via activerecord type caster or directly in the query? This will raise an error in PG:
|
@dmitry ActiveRecord casts (basically |
ActiveRecord is basically cast the value using with_comments = Post.joins(:comments).where(comments: { id: 'foo' })
with_comments.to_sql,
#=> SELECT "posts".* FROM "posts" INNER JOIN "comments" ON "comments"."post_id" = "posts"."id" WHERE "comments"."id" = 0 However, ActiveRecord does not cast when using nested joins and Hash value: with_likes = Post.joins(comments: :likes).where(likes: { id: 'foo' })
with_likes.to_sql
#=> SELECT "posts".* FROM "posts" INNER JOIN "comments" ON "comments"."post_id" = "posts"."id" INNER JOIN "likes" ON "likes"."comment_id" = "comments"."id" WHERE "likes"."id" = 'foo' I was wondering if this was the intended behavior of ActiveRecord, so I reported an issue. |
@sinsoku it's most likely a bug. feel free to submit a PR. I looked through the tests and didn't find any case for not serializing integer IDs to integers. |
Unfortunately, a11a8ff had no effect as long as using bind param, and was not tested. This ensures making the intent of a11a8ff, which fall back to type casting from the connection adapter. Fixes rails#35205. ``` % ARCONN=postgresql bundle exec ruby -w -Itest test/cases/relation/where_test.rb -n test_type_casting_nested_joins Using postgresql Run options: -n test_type_casting_nested_joins --seed 55730 # Running: E Error: ActiveRecord::WhereTest#test_type_casting_nested_joins: ActiveRecord::StatementInvalid: PG::InvalidTextRepresentation: ERROR: invalid input syntax for integer: "2-foo" rails test test/cases/relation/where_test.rb:30 Finished in 0.245778s, 4.0687 runs/s, 0.0000 assertions/s. 1 runs, 0 assertions, 0 failures, 1 errors, 0 skips ```
Steps to reproduce
Expected behavior
I expected "foo" to be cast for bigint column.
Actual behavior
I get a query that uses string in bigint column.
If you use PostgreSQL, then it occurs
ActiveRecord::StatementInvalid
.System configuration
Rails version: master(25d8afb)
Ruby version: ruby 2.6.1p33 (2019-01-30 revision 66950) [x86_64-darwin15]
The text was updated successfully, but these errors were encountered: