at.*_*at. 5 postgresql cast heroku postgresql-9.3 rails
我在试图改变类型的列时,这个错误time
类型为timestamp
:
PG::CannotCoerce: ERROR: cannot cast type time without time zone to timestamp without time zone
Run Code Online (Sandbox Code Playgroud)
现有时间如何转换对我来说并不重要,但我确实需要更改此列。我怎么能强制这个或投这个?
这是 SQL 语句:
ALTER TABLE "students" ALTER COLUMN "time_since_missing_schedule_notification" TYPE timestamp USING CAST(time_since_missing_schedule_notification AS timestamp)
Run Code Online (Sandbox Code Playgroud)
我正在使用 Rails/ActiveRecord,这是生成上述 SQL 语句的 Ruby 代码:
change_column :students, :time_since_missing_schedule_notification, 'timestamp USING CAST(time_since_missing_schedule_notification AS timestamp without time zone)'
Run Code Online (Sandbox Code Playgroud)
当我连接到 Heroku 的 PostgreSQL 服务器时,我得到以下版本:
psql (9.3.1, server 9.2.7)
Run Code Online (Sandbox Code Playgroud)
由于时间没有date
组件,您需要提供日期。您可以在语句中添加date
和time
值ALTER TABLE
:
ALTER TABLE students ALTER COLUMN time_since_missing_schedule_notification
TYPE timestamp USING ('2000-1-1'::date + time_since_missing_schedule_notification)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
19054 次 |
最近记录: |