Larson B
Larson B

Reputation: 309

Rails - migrate from hstore to jsonb

I use Postgresql. I tried the following migration to cast a field from hstore to jsonb in Rails.

change_column :table_name, :field_name, 'jsonb USING CAST(field_name AS jsonb)'

I get the following exception:

PG::DatatypeMismatch: ERROR: default for column "field_name" cannot be cast automatically to type jsonb.

Upvotes: 2

Views: 2776

Answers (1)

Larson B
Larson B

Reputation: 309

I just figured out the issue. The exception clearly states that there is a default value which it is not able to automatically cast.

There was a default value for the column which I tried to cast.

Changing to the following migration worked.

change_column_default :table_name, :field_name, nil
change_column :table_name, :field_name, 'jsonb USING CAST(field_name AS jsonb)'

Upvotes: 10

Related Questions