# Change\_column

**URL:** <https://www.ruby-forum.com/t/change-column/124405>\
**Category:** Rails\
**Created:** [December 28, 2007, 12:29pm UTC](https://www.ruby-forum.com/t/change-column/124405 "2007-12-28T12:29:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![suneeta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b77776/32.png) [@suneeta](https://www.ruby-forum.com/u/suneeta)\
**Post date:** [December 28, 2007, 12:29pm UTC](https://www.ruby-forum.com/t/change-column/124405/1 "2007-12-28T12:29:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![suneeta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b77776/32.png) [@suneeta](https://www.ruby-forum.com/u/suneeta)\
**Post date:** [December 28, 2007, 1:13pm UTC](https://www.ruby-forum.com/t/change-column/124405/2 "2007-12-28T13:13:24Z")

</div>

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 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![suneeta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b77776/32.png) [@suneeta](https://www.ruby-forum.com/u/suneeta)\
**Post date:** [December 29, 2007, 5:09am UTC](https://www.ruby-forum.com/t/change-column/124405/3 "2007-12-29T05:09:37Z")

</div>

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 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

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

---

<div class="post-metadata">

**Author:** ![suneeta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b77776/32.png) [@suneeta](https://www.ruby-forum.com/u/suneeta)\
**Post date:** [December 29, 2007, 5:34am UTC](https://www.ruby-forum.com/t/change-column/124405/4 "2007-12-29T05:34:06Z")

</div>

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
