Change_column

Hi…
I’ve a table named software_cis. I need to change the type of the
column ci_class in this table from string to integer. For this i
generated a migration file named ChangeCiClassToInteger.

class ChangeCiClassToInteger < ActiveRecord::Migration
def self.up
change_column :software_cis, :ci_class, :integer
end

def self.down
raise ActiveRecord::IrreversibleMigration
end
end

When i run the migration, it gets terminated and generates a message:

rake aborted!
RuntimeError: ERROR C42846 Mcannot cast type character varying to
integer Fparse_expr.c L1533 Rtypecast_expression: UPDATE software_cis
SET ci_class_ar_tmp = CAST(ci_class AS integer)

Anyone please help to find the solution…

Regards
Suneeta

Try to specify the :limit and :default for the migration and run it
again.

Check for the rows that contain some string values in the column
ci_class

On Dec 28, 4:29 pm, Suneeta Km [email protected]

Kiran Soumya wrote:

Try to specify the :limit and :default for the migration and run it
again.

Check for the rows that contain some string values in the column
ci_class

On Dec 28, 4:29 pm, Suneeta Km [email protected]

Still its not working… Is it not possible to convert the column type
from string to integer?

On Dec 28, 2007, at 10:09 PM, Suneeta Km wrote:

Still its not working… Is it not possible to convert the column
type
from string to integer?

The original error you posted

RuntimeError: ERROR C42846 Mcannot cast type character varying to
integer Fparse_expr.c L1533 Rtypecast_expression: UPDATE software_cis
SET ci_class_ar_tmp = CAST(ci_class AS integer)

strongly suggests that there are values in the ci_class column that
cannot be cast as integer. Either they are spaces, characters, or
something. Most databases have some sort of IS_NUMERIC function you
can use to verify the contents of the columns. In the database query
tool that you use, run something like

select * from software_cis where is_numeric(ci_class) = 0

You’ll have to adapt that for your specific database. I’m thinking
you should have at least one row returned.

Peace,
Phillip