21

There is a good question here I want to elaborate on. I am trying to convert a column in my database form a string to an integer.

I thought the conversion would be pretty straight forwrad. Currently my strings are

["10", "12", "125", "135", "140", ...]

My migration file includes:

def change
    change_column :table_name, :product_code, :integer
end

Rails tries this but Postgresql thows back an error.

PG::Error: ERROR: column "product_code" cannot be cast automatically to type integer
HINT: Specify a USING expression to perform the conversion.

I am not sure how I use this 'USING' expression in my rails migration.

So I thought the conversion would be pretty straight forward. What should I use as the USING expression?

2
  • 1
    Thanks for asking this question. Keep it up Commented Jun 11, 2014 at 23:32
  • I have been reading a book about SQL and now am shaking my head. Ofcourse you can't just convert strings to integers without using some SQL statements to say how you want to CAST it. Wish I knew all about SQL a long time ago. Commented Oct 18, 2015 at 2:11

3 Answers 3

32
change_column :table_name, :product_code,
  'integer USING CAST(product_code AS integer)'

Source: http://makandracards.com/makandra/18691-postgresql-vs-rails-migration-how-to-change-columns-from-string-to-integer

Sign up to request clarification or add additional context in comments.

4 Comments

Doesn't seem to work with blank strings. invalid input syntax for integer: ""
Well, that makes sense. What integer would represent a blank string?
Does this reset the index? After performing such a migration I'm getting wrongly ordered queries.
You should specify an explicit ORDER BY if you care about ordering
19

Adjusted code to support converting blank strings, too:

change_column :table_name, :product_code, 
  "integer USING NULLIF(product_code, '')::int"

Empty string becomes NULL, which becomes 0 on type conversion, which is probably the best you can do in this situation.

1 Comment

Works like a charm. I had the same issue - the migration from stackoverflow.com/a/22394425/1513205 works great on an empty database - but our database already have empty string values in it.
0

When you write Rails migrations to convert a string column to an integer you'd usually write like this:

change_column :table_name, :column_name, :integer

You might get this:

PG::DatatypeMismatch: ERROR:  column "column_name" cannot be cast automatically to type integer
HINT:  Specify a USING expression to perform the conversion.

The "hint" basically tells you that you need to confirm you want this to happen, and how data shall be converted. Just say this in your migration:

change_column :table_name, :column_name, 'integer USING CAST(column_name AS integer)'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.