Rails 4, migration to change datatype of column from daterange to tsrange causing PG::DatatypeMismatch: ERROR:

Viewed 1614

I'm trying to change a column of type daterange to tsrange (I realized I need time as well as date) using a vanilla Rails migration

def self.up
  change_column :events, :when, :tsrange
end

After running rake db:migrate the error is

PG::DatatypeMismatch: ERROR: column "when" cannot be cast automatically to type tsrange HINT: Specify a USING expression to perform the conversion. : ALTER TABLE "events" ALTER COLUMN "when" TYPE tsrange

I tried following the hint and used the following

def self.up
  change_column :events, :when, :tsrange, 'tsrange USING CAST(when AS tsrange)'
end

but then got

no implicit conversion of Symbol into Integer

From what I can tell, USING CAST is mainly meant for use with ints. Assuming I don't want to drop and then recreate the column, what do you have to specify to alter the type from daterange to tsrange?

I'm using

  • Rails 4.0.1
  • ruby-2.0.0-p247
  • psql (9.2.4)

Some background, daterange and tsrange were introduced to Rails 4 in the following PR: https://github.com/rails/rails/pull/7345. Thanks.

1 Answers
Related