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.

On Mon, Oct 22, 2012 at 2:51 AM, Dmitry M. [email protected]
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 S. ------------------------ [email protected]

twitter: @hassan

On Monday, October 22, 2012 10:51:21 AM UTC+1, Dmitry M. 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 only
does 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

On Tuesday, October 23, 2012 3:32:20 AM UTC+13, Hassan S. wrote:

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.

On Tue, Oct 23, 2012 at 3:48 AM, Dmitry M. [email protected]
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 :slight_smile:

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 S. ------------------------ [email protected]

twitter: @hassan