轨道与Postgres:迁移到change

编程入门 行业动态 更新时间:2024-10-28 22:31:49
本文介绍了轨道与Postgres:迁移到change_colomn会产生错误“无法转换为没有时区的timestamp类型”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

Rails迁移将 deleted_at时间列转换为日期时间列失败。关于如何解决这个问题的任何想法?如果需要的话,它是Postgres的全新安装。

A Rails migration to turn a "deleted_at" time column to a datetime column failed. Any ideas on how to solve this? It's a fresh install of Postgres if that is relevant.

-- change_column(:products, :deleted_at, :datetime) PGError: ERROR: column "deleted_at" cannot be cast to type timestamp without time zone : ALTER TABLE "products" ALTER COLUMN "deleted_at" TYPE timestamp

推荐答案

您不能随时间更改字段的类型( datetime) ,因为值无法转换-数据库不知道日期。

You can't alter a field's type from time to timestamp ("datetime"), because the values couldn't be converted -- the database doesn't know the date.

但是,您可以删除并重新输入-创建列:

You can, however, drop and re-create the column:

ALTER TABLE products DROP COLUMN deleted_at; ALTER TABLE products ADD COLUMN deleted_at timestamp;

或者如果此字段设置为NOT NULL,则应该执行以下操作:

Or if this field was set to NOT NULL, you should instead do:

ALTER TABLE products ADD COLUMN deleted_at timestamp NOT NULL;

但是,如果您坚持像Sean这样在表中保留假值,则可以使用ALTER ... TYPE ...的用法如下:

But if you insist on retaining fake values in this table like Sean, you can use ALTER...TYPE...USING like this:

ALTER TABLE products ALTER COLUMN deleted_at TYPE timestamp USING CASE WHEN deleted_at IS NOT NULL THEN timestamp '1970-01-01 00:00:00' END; -- Or: ALTER TABLE products ALTER COLUMN deleted_at TYPE timestamp USING date '1970-01-01' + deleted_at;

更多推荐

轨道与Postgres:迁移到change

本文发布于:2023-10-19 11:18:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1507332.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:轨道   Postgres   change

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!