Mysql 改变';时间';列至';日期时间';

Mysql 改变';时间';列至';日期时间';,mysql,ruby-on-rails,datetime,Mysql,Ruby On Rails,Datetime,在Rails 3应用程序中,我有一个表订阅计划,其中有一个“time”类型的列 示例记录如下所示的LOKK: #<Subscription::Schedule id: 3504, subscription_id: 4961, scheduled_at: "2000-01-01 01:11:00", primary: true, created_at: "2015-09-28 01:13:34", updated_at: "2015-09-28 01:13:34", active: true

在Rails 3应用程序中,我有一个表订阅计划,其中有一个“time”类型的列

示例记录如下所示的LOKK:

#<Subscription::Schedule id: 3504, subscription_id: 4961, scheduled_at: "2000-01-01 01:11:00", primary: true, created_at: "2015-09-28 01:13:34", updated_at: "2015-09-28 01:13:34", active: true>
add_column :subscription_schedules, :temp_scheduled_at, :datetime
Subscription::Schedule.select {|s| s.subscription}.each do |schedule|
        schedule.temp_scheduled_at = schedule.scheduled_at
      schedule.scheduled_at = nil
      begin
          schedule.save!
      rescue => e
        puts schedule.id
      end
    end

change_column :subscription_schedules, :scheduled_at, :datetime
#<Subscription::Schedule id: 2555, subscription_id: 4102, scheduled_at: "2000-01-01 05:00:00", primary: true, created_at: "2015-06-23 16:12:01", updated_at: "2015-06-23 16:12:01", active: true>
不幸的是,问题就从这里开始。调用
Subscription::Scheudule.all
之后,我得到

Invalid date in field 'scheduled_at': 2001-00-00 00:00:00
我真的不知道是什么导致了这个错误,在更改带有“无效日期”的列类型记录之前,如下所示:

#<Subscription::Schedule id: 3504, subscription_id: 4961, scheduled_at: "2000-01-01 01:11:00", primary: true, created_at: "2015-09-28 01:13:34", updated_at: "2015-09-28 01:13:34", active: true>
add_column :subscription_schedules, :temp_scheduled_at, :datetime
Subscription::Schedule.select {|s| s.subscription}.each do |schedule|
        schedule.temp_scheduled_at = schedule.scheduled_at
      schedule.scheduled_at = nil
      begin
          schedule.save!
      rescue => e
        puts schedule.id
      end
    end

change_column :subscription_schedules, :scheduled_at, :datetime
#<Subscription::Schedule id: 2555, subscription_id: 4102, scheduled_at: "2000-01-01 05:00:00", primary: true, created_at: "2015-06-23 16:12:01", updated_at: "2015-06-23 16:12:01", active: true>
#

我建议您在mysql查询中使用它。您可以在mysql查询中将时间更改为日期时间,并将其称为新列名。与编程方式相比,它非常容易。前-

select schedule_at, now() as temp_scheduled_at from tablename; 

您可以在已包含日期时间值的列中读取更多日期时间函数。您可以发布当前输出和所需输出吗?它看起来像,但在mysql中是“time”列,在rails中也只存储小时、分钟和秒。当前输出如上所示,我想要的是datetime列,稍后我将其填入信息。您想要任何与该时间关联的虚拟日期吗?我想要(手动)转换时间,我不知道您在本上下文中所说的虚拟日期是什么意思。我的意思是,您只得到了时间而没有日期。那么我该如何将日期与时间相加呢