mysql - 日期从 ActiveRecord 恢复到 2000-01-01 到 MySQL

标签 mysql ruby activerecord time ruby-on-rails-3.1

我正在尝试在 MySQL 表中保存时间。但是,日期恢复为 2000-01-01。

1.9.2p320 :036 > vv = Visitor.new
 => #<Visitor id: nil, ip_address: nil, num_day_visits: nil, last_visit: nil> 
1.9.2p320 :037 > vv.last_visit = Time.now; vv.ip_address = "3.3.3.3"
 => "3.3.3.3" 
1.9.2p320 :038 > vv.num_day_visits = 1
 => 1 
1.9.2p320 :039 > vv
 => #<Visitor id: nil, ip_address: "3.3.3.3", num_day_visits: 1, last_visit: "2012-10-11 01:31:04"> 
1.9.2p320 :040 > vv.save
  SQL (0.2ms)  BEGIN
  SQL (0.7ms)  INSERT INTO `visitors` (`ip_address`, `last_visit`, `num_day_visits`) VALUES (?, ?, ?)  [["ip_address", "3.3.3.3"], ["last_visit", 2012-10-11 01:31:04 -0400], ["num_day_visits", 1]]
   (0.5ms)  COMMIT
 => true 
1.9.2p320 :042 > vv
 => #<Visitor id: 1199, ip_address: "3.3.3.3", num_day_visits: 1, last_visit: "2012-10-11 01:31:04"> 
1.9.2p320 :043 > Visitor.find(:all,:conditions=>{:ip_address => "3.3.3.3"})
  Visitor Load (1.4ms)  SELECT `visitors`.* FROM `visitors` WHERE `visitors`.`ip_address` = '3.3.3.3'
 => [#<Visitor id: 1199, ip_address: "3.3.3.3", num_day_visits: 1, last_visit: "2000-01-01 05:31:04">] 

所以当我检索记录时,日期是 2000-01-01。

MySQL中的表:

mysql> describe visitors ;
+----------------+--------------+------+-----+---------+----------------+
| Field          | Type         | Null | Key | Default | Extra          |
+----------------+--------------+------+-----+---------+----------------+
| id             | int(11)      | NO   | PRI | NULL    | auto_increment |
| ip_address     | varchar(255) | NO   |     | NULL    |                |
| num_day_visits | int(11)      | NO   |     | NULL    |                |
| last_visit     | time         | NO   |     | NULL    |                |
+----------------+--------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

更新:我创建了一个玩具表来玩弄从时间到日期时间的转换。这是发生了什么:

mysql> select * from example ;
+----+----------+
| id | mytime   |
+----+----------+
|  1 | 11:13:00 |
+----+----------+
1 row in set (0.00 sec)
mysql> alter table example change mytime mytime datetime;
Query OK, 1 row affected, 1 warning (0.02 sec)
Records: 1  Duplicates: 0  Warnings: 1

mysql> select * from example ;
+----+---------------------+
| id | mytime              |
+----+---------------------+
|  1 | 0000-00-00 00:00:00 |
+----+---------------------+
1 row in set (0.00 sec)

这样就破坏了值(value)。我试图通过将表更改为时间并使用新行来返回。重新开始:

mysql> select * from example ;
+----+----------+
| id | mytime   |
+----+----------+
|  2 | 11:13:00 |
+----+----------+
1 row in set (0.00 sec)
mysql> ALTER TABLE example CHANGE mytime mytime DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP();
ERROR 1067 (42000): Invalid default value for 'mytime'
mysql> ALTER TABLE example CHANGE mytime mytime DATETIME NOT NULL DEFAULT CURRENT_DATE();
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CURRENT_DATE()' at line 1

显然,this is hard .

最佳答案

这是因为您的 last_visit 列是 time 而不是 datetime 类型。最初我假设 mysql 只是使用 01-01-2000 作为内部表示时间字段的某种默认日期,但看起来 01-01-2000 是 rails 而不是 mysql 所做的。看Represent a time with no date in ruby

关于mysql - 日期从 ActiveRecord 恢复到 2000-01-01 到 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12832857/

相关文章:

mysql - 如何计算mysql innodb表中一行的磁盘空间使用情况

ruby - 散列值大小错误

Windows 10 上的 Ruby sqlite3 gem 安装问题

ruby-on-rails - 查看 Rails 迁移输出的方法

php - 从查询转换为 Yii2 的 ModelSearch

javascript - 如何删除Textarea中行之间的行间距?

mysql - mysql中区分大小写的字符串比较

mysql - 无法使用 IF ... THEN 语句执行查询

ruby-on-rails - rails : path of file

ruby-on-rails - 在 ActiveRecord 模型中设置默认随机列值