铁轨和Postgres:迁移到change_colomn 会出现错误“无法转换为没有时区的时间戳类型”

发布于 2024-10-29 05:28:59 字数 321 浏览 1 评论 0原文

将“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

递刀给你 2024-11-05 05:28:59

在 Rails 中,这看起来像

class ChangeStatusUpdatedAtToDateTime < ActiveRecord::Migration
  def up
    remove_column :bookings, :status_updated_at
    add_column :bookings, :status_updated_at, :datetime
  end

  def down
    remove_column :bookings, :status_updated_at
    add_column :bookings, :status_updated_at, :time
  end
end

如果你有想要传输的数据,你可以使用以下代码(未经测试!):

class ChangeStatusUpdatedAtToDateTime < ActiveRecord::Migration
  def up
    add_column :bookings, :temp_status_updated_at, :datetime
    Booking.update_all("temp_status_updated_at = updated_at")
    remove_column :bookings, :status_updated_at
    rename_column :bookings, :temp_status_updated_at, :status_updated_at
  end

  def down
    add_column :bookings, :temp_status_updated_at, :time
    Booking.update_all("temp_status_updated_at = updated_at")
    remove_column :bookings, :status_updated_at
    rename_column :bookings, :temp_status_updated_at, :status_updated_at
  end
end

In Rails this would look something like

class ChangeStatusUpdatedAtToDateTime < ActiveRecord::Migration
  def up
    remove_column :bookings, :status_updated_at
    add_column :bookings, :status_updated_at, :datetime
  end

  def down
    remove_column :bookings, :status_updated_at
    add_column :bookings, :status_updated_at, :time
  end
end

If you had data you wanted to transfer you could use the following code (not tested!):

class ChangeStatusUpdatedAtToDateTime < ActiveRecord::Migration
  def up
    add_column :bookings, :temp_status_updated_at, :datetime
    Booking.update_all("temp_status_updated_at = updated_at")
    remove_column :bookings, :status_updated_at
    rename_column :bookings, :temp_status_updated_at, :status_updated_at
  end

  def down
    add_column :bookings, :temp_status_updated_at, :time
    Booking.update_all("temp_status_updated_at = updated_at")
    remove_column :bookings, :status_updated_at
    rename_column :bookings, :temp_status_updated_at, :status_updated_at
  end
end
娇妻 2024-11-05 05:28:59

您无法将字段的类型从时间更改为时间戳(“日期时间”),因为无法转换值 - 数据库不知道日期。

但是,您可以删除并重新创建该列:

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

或者如果该字段设置为 NOT NULL,您应该这样做:

ALTER TABLE products ADD COLUMN deleted_at timestamp NOT NULL;

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

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;

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;

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

ALTER TABLE products ADD COLUMN deleted_at timestamp NOT NULL;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文