AFOC
AFOC

Reputation: 844

Rails: Change existing integer column to array data type

How do I change an integer column (which already has values) to an integer array column? When I try to migrate this:

def change
  change_column :cards, :value, :integer, array: true, default: []
end

I get this error:

PG::DatatypeMismatch: ERROR:  column "value" cannot be cast automatically to type integer[]
HINT:  You might need to specify "USING value::integer[]"

So I tried this:

change_column :cards, :value, :integer, array: true, default: [], using: 'value::integer[]'

Then I get this error:

PG::CannotCoerce: ERROR:  cannot cast type integer to integer[]

Upvotes: 0

Views: 545

Answers (1)

AFOC
AFOC

Reputation: 844

After digging around for postgres casting functions & trying several things that didn't work, I found the answer.

using: 'ARRAY[value]::INTEGER[]'

The hint was close, but off by just enough to be confusing.

Upvotes: 2

Related Questions