Dmitry Maksyoma
2012-Oct-22 09:51 UTC
ActiveRecord::Base.connection.select_all() returns integers as strings
Hi, I''m doing a select_all("SELECT attnum, attname FROM pg_attribute") and the resulting hash has attnum value as string, despite it being an integer column in the database. Am I missing some switch that would turn on the conversion? Thanks. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/aK8JxPxD4UcJ. For more options, visit https://groups.google.com/groups/opt_out.
Hassan Schroeder
2012-Oct-22 14:31 UTC
Re: ActiveRecord::Base.connection.select_all() returns integers as strings
On Mon, Oct 22, 2012 at 2:51 AM, Dmitry Maksyoma <ledestin-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m doing a select_all("SELECT attnum, attname FROM pg_attribute") and the > resulting hash has attnum value as string, despite it being an integer > column in the database. Am I missing some switch that would turn on the > conversion?What db version and database adapter are you using? SELECT attnum, attname FROM pg_attribute LIMIT 10 => [{"attnum"=>1, "attname"=>"proname"}, {"attnum"=>2, "attname"=>"pronamespace"}, {"attnum"=>3, "attname"=>"proowner"}, {"attnum"=>4, "attname"=>"prolang"}, {"attnum"=>5, "attname"=>"procost"}, {"attnum"=>6, "attname"=>"prorows"}, {"attnum"=>7, "attname"=>"provariadic"}, {"attnum"=>8, "attname"=>"proisagg"}, {"attnum"=>9, "attname"=>"proiswindow"}, {"attnum"=>10, "attname"=>"prosecdef"}] That''s a quick test using activerecord-jdbcpostgresql-adapter (1.2.2) with Rails 3.2.8, JRuby 1.7.0 preview2, postgres 9.1.3. -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://about.me/hassanschroeder twitter: @hassan -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Frederick Cheung
2012-Oct-22 19:25 UTC
Re: ActiveRecord::Base.connection.select_all() returns integers as strings
On Monday, October 22, 2012 10:51:21 AM UTC+1, Dmitry Maksyoma wrote:> > Hi, > > I''m doing a select_all("SELECT attnum, attname FROM pg_attribute") and the > resulting hash has attnum value as string, despite it being an integer > column in the database. Am I missing some switch that would turn on the > conversion? > > When you do select_all Rails doesn''t do any typecasting (since it onlydoes that when it has a correspond model object) so what you''ll get depends on your database driver. The mysql driver returned everything as strings for example but the mysql2 driver returns integers, dates etc as appropriate. I don''t know how things play out in postgres-land though. Fred -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/1qDXujtnSVMJ. For more options, visit https://groups.google.com/groups/opt_out.
Dmitry Maksyoma
2012-Oct-23 10:48 UTC
Re: ActiveRecord::Base.connection.select_all() returns integers as strings
On Tuesday, October 23, 2012 3:32:20 AM UTC+13, Hassan Schroeder wrote:> > On Mon, Oct 22, 2012 at 2:51 AM, Dmitry Maksyoma <lede...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org<javascript:>> > wrote: > > > I''m doing a select_all("SELECT attnum, attname FROM pg_attribute") and > the > > resulting hash has attnum value as string, despite it being an integer > > column in the database. Am I missing some switch that would turn on the > > conversion? > > What db version and database adapter are you using?Postgres 9.1.6 and postgresql adapter. SELECT attnum, attname FROM pg_attribute LIMIT 10> => [{"attnum"=>1, "attname"=>"proname"}, {"attnum"=>2, > "attname"=>"pronamespace"}, {"attnum"=>3, "attname"=>"proowner"}, > {"attnum"=>4, "attname"=>"prolang"}, {"attnum"=>5, > "attname"=>"procost"}, {"attnum"=>6, "attname"=>"prorows"}, > {"attnum"=>7, "attname"=>"provariadic"}, {"attnum"=>8, > "attname"=>"proisagg"}, {"attnum"=>9, "attname"=>"proiswindow"}, > {"attnum"=>10, "attname"=>"prosecdef"}] > > That''s a quick test using activerecord-jdbcpostgresql-adapter (1.2.2) > with Rails 3.2.8, JRuby 1.7.0 preview2, postgres 9.1.3.The adapter you''re using is for JRuby, I''m using native Ruby. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/xqj9itGQufsJ. For more options, visit https://groups.google.com/groups/opt_out.
Hassan Schroeder
2012-Oct-23 11:27 UTC
Re: ActiveRecord::Base.connection.select_all() returns integers as strings
On Tue, Oct 23, 2012 at 3:48 AM, Dmitry Maksyoma <ledestin-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> The adapter you''re using is for JRuby, I''m using native Ruby.Right, just making the point that it''s not a PostgreSQL issue, it''s that the adapter you''re using is broken. Or at least not behaving as you''d expect :-) And I just created a 1.9.3/MRI `rails new -d postgresql` app which installed the pg 0.14.1 gem, and it returns integers as strings, so at least the problem is reproducible. -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://about.me/hassanschroeder twitter: @hassan -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.