gpt4 book ai didi

postgresql - 如何将我的 Rails 数据类型更改为 PostGres bigint?

转载 作者:行者123 更新时间:2023-11-29 12:25:50 28 4
gpt4 key购买 nike

我正在尝试运行以下 Rails 迁移,使用 Rails 4.2.4 和 PostGresql ......

class ChangeTimeInMsColInMyObjectTimes < ActiveRecord::Migration
def change
change_column :my_object_times, :time_in_ms, :integer, :limit => 8
end
end

但它会导致以下错误。我

rake db:migrate
== 20160613195631 ChangeTimeInMsColInMyObjectTimes: migrating =====================
-- change_column(:my_object_times, :time_in_ms, :integer, {:limit=>8})
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

PG::DatatypeMismatch: ERROR: column "time_in_ms" cannot be cast automatically to type bigint
HINT: You might need to specify "USING time_in_ms::bigint".
: ALTER TABLE "my_object_times" ALTER COLUMN "time_in_ms" TYPE bigint
/Users/davea/.rvm/gems/ruby-2.3.0@global/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `async_exec'
/Users/davea/.rvm/gems/ruby-2.3.0@global/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `block in execute'
/Users/davea/.rvm/gems/ruby-2.3.0@global/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract_adapter.rb:472:in `block in log'
/Users/davea/.rvm/gems/ruby-2.3.0@global/gems/activesupport-4.2.5.1/lib/active_support/notifications/instrumenter.rb:20:in `instrument'

我需要在迁移中使用的正确语法是什么?

谢谢,-戴夫

最佳答案

如错误消息所述,您无法将 interval 自动转换为 bigint。您也不能简单地转换它,因此建议的 using time_in_ms::bigint 也不会起作用。

这个建议部分是正确的,在更改列的类型时手动整理类型转换的通常方法是在 ALTER TABLE 中使用 USING 子句,因此您需要做的就是找出要使用哪个表达式来转换interval 到毫秒数作为 bigint

在数据库中对时间类型进行切片和切 block 的常用方法是使用 extract :

9.9.1. EXTRACT, date_part

EXTRACT(field FROM source)

The extract function retrieves subfields such as year or hour from date/time values. [...] The extract function returns values of type double precision.

在这种情况下,字段 是您感兴趣的时间部分,epoch 可能是您所追求的:

epoch
[...] for interval values, the total number of seconds in the interval

extract 为您提供 double 秒数,因此您必须自己将秒数转换为毫秒数。像这样的东西:

using extract(epoch from time_in_ms) * 1000

应该可以解决问题。在迁移中,这将是:

change_column :my_object_times, :time_in_ms, :integer, :limit => 8, :using => 'extract(epoch from time_in_ms) * 1000'

change_column :my_object_times, :time_in_ms, :bigint, :using => 'extract(epoch from time_in_ms) * 1000'

关于postgresql - 如何将我的 Rails 数据类型更改为 PostGres bigint?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37798237/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com