mysql adapter with "as" in query: type lost

Here is something that "works" under SQL Server (with freetds / odbc) but not mysql.

When renaming columns using the SQL 'as' construct with the mysql adapter, the type is not always preserved.

# script/console

SomeModel.find(:first, :select => 'integer_column').integer_column.class

=> Fixnum

SomeModel.find(:first, :select => 'integer_column as width').width.class

=> String

More detailed:

# cat db/migrate/005_create_noodles.rb class CreateSomeModel < ActiveRecord::Migration   def self.up     create_table :some_models do |t|       t.integer :integer_column     end   end

  def self.down     drop_table :some_models   end end

# rake db:migrate # script/console>> SomeModel.create(:integer_column => 4) => #<SomeModel id: 1, integer_column: 4>

SomeModel.find(:first, :select => 'integer_column as width').width

=> "4"

_.class

=> String

Under SQL Server get "Fixnum" instead of "String". Fixnum makes more sense to me.

I get the impression the Mysql::Result#each_hash method is responsible. I tried with both Ruby and C implementation do the same.

What should it be?

(I posted this to the Ruby-on-Rails mailing list, but without replies)

Stephan

When renaming columns using the SQL 'as' construct with the mysql adapter, the type is not always preserved.

IIRC the mysql drivers (both C and pure-ruby) returns those (all?) values as strings. When AR knows the column it can typecast the values itself, but for piggybacked values like this, we can't. This is an upstream bug and should probably be fixed there.

Michael Koziarski wrote:

IIRC the mysql drivers (both C and pure-ruby) returns those (all?) values as strings. When AR knows the column it can typecast the values itself, but for piggybacked values like this, we can't. This is an upstream bug and should probably be fixed there.

Ok, so it is not intended to be this way, and the SQL Server driver is doing the right thing.

Thanks; I'll try to find out from "upstream".

Stephan