LoginSignup
3
0

More than 5 years have passed since last update.

カラムの型を time から datetime に変更する

Posted at

time 型のカラムを datetime 型に変更しようとした際に以下のエラーがでました。

$ rails db:migrate
(中略)
PG::DatatypeMismatch: ERROR:  column "カラム名" cannot be cast automatically to type timestamp without time zone
HINT:  You might need to specify "USING カラム名::timestamp without time zone".
: ALTER TABLE "テーブル名" ALTER COLUMN "カラム名" TYPE timestamp

結論からいうと、time 型から datetime 型への変換はできないので、(time 型のカラムの日付がわからないため)

  1. time 型のカラムを削除
  2. datetime 型で該当カラムをもう一度追加

とすることで解決しました。

カラムの追加と削除については、Ruby on Rails カラムの追加と削除 という記事にわかりやすくまとめてあります。

参考

Rails & Postgres: Migration to change_colomn gives error “cannot be cast to type timestamp without time zone”

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0