铁轨和Postgres:迁移到change_colomn 会出现错误“无法转换为没有时区的时间戳类型”
将“deleted_at”时间列转换为日期时间列的 Rails 迁移失败。关于如何解决这个问题有什么想法吗?如果相关的话,这是 Postgres 的全新安装。
-- 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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Rails 中,这看起来像
如果你有想要传输的数据,你可以使用以下代码(未经测试!):
In Rails this would look something like
If you had data you wanted to transfer you could use the following code (not tested!):
您无法将字段的类型从时间更改为时间戳(“日期时间”),因为无法转换值 - 数据库不知道日期。
但是,您可以删除并重新创建该列:
或者如果该字段设置为 NOT NULL,您应该这样做:
但是,如果您坚持像 Sean 一样在此表中保留假值,则可以使用 ALTER...TYPE ...像这样使用:
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:
Or if this field was set to NOT NULL, you should instead do:
But if you insist on retaining fake values in this table like Sean, you can use ALTER...TYPE...USING like this: