gpt4 book ai didi

ruby-on-rails - 将时间戳添加到现有表

转载 作者:数据小太阳 更新时间:2023-10-29 06:16:54 25 4
gpt4 key购买 nike

我需要将时间戳 (created_at & updated_at) 添加到现有表中。我尝试了以下代码,但没有用。

class AddTimestampsToUser < ActiveRecord::Migration
def change_table
add_timestamps(:users)
end
end

最佳答案

时间戳助手仅在 create_table block 中可用。您可以通过手动指定列类型来添加这些列:

class AddTimestampsToUser < ActiveRecord::Migration
def change_table
add_column :users, :created_at, :datetime, null: false
add_column :users, :updated_at, :datetime, null: false
end
end

虽然这不像您在上面指定的 add_timestamps 方法那样简洁,但 Rails 仍会将这些列视为时间戳列,并正常更新值。

关于ruby-on-rails - 将时间戳添加到现有表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7542976/

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