Setting MySQL DATETIME column defaults in Rails_MySQL
程序员文章站
2022-04-25 10:24:13
...
Starting in MySQL 5.6.5, datetime columns can have an actual useful default of CURRENT_TIMESTAMP and MySQL will auto-populate the columns as necessary. This is incredibly handy if you ever do bulk updates in SQL, now you don’t need to remember to set updated_at! Inserting records manually will auto-populate those columns too. Let’s try it:
def upcreate_table :rows do |t|t.integer :valuet.datetime :created_at, null: false, default: "CURRENT_TIMESTAMP"t.datetime :updated_at, null: false, default: "CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"endend
Run that and we’ll see this:
ActiveRecord::StatementInvalid: Mysql2::Error: Invalid default value for 'created_at': CREATE TABLE `rows` (`id` int(11) DEFAULT NULL auto_increment PRIMARY KEY, `value` int(11) NULL, `created_at` datetime DEFAULT 'CURRENT_TIMESTAMP' NOT NULL, `updated_at` datetime DEFAULT 'CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP' NOT NULL) ENGINE=InnoDB
Notice that Rails quotes the default value, making it invalid. We can bypass this by using a custom type to define all the special logic we need and use the genericcolumn
definition method:
CREATE_TIMESTAMP = 'DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP'UPDATE_TIMESTAMP = 'DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'def upcreate_table :rows do |t|t.integer :valuet.column :created_at, CREATE_TIMESTAMPt.column :updated_at, UPDATE_TIMESTAMP endend
Big Caveat: you must make sure your database’s timezone is set correctly. MySQL defaults to the system’s timezone and we set our system timezone to Pacific so everything should work fine for us.
$ mysqlmysql> select @@time_zone;+-------------+| @@time_zone |+-------------+| SYSTEM|+-------------+
Defined like that, those columns will be populated and updated any time rows are touched, not just when Rails does it.
推荐阅读
-
使用Mysql5.x以上版本出现报错#1929 Incorrect datetime value: '''' for column ''createtime''
-
使用Mysql5.x以上版本出现报错#1929 Incorrect datetime value: '''' for column ''createtime''的快速解决方法
-
使用Mysql5.x以上版本出现报错#1929 Incorrect datetime value: '''' for column ''createtime''
-
使用Mysql5.x以上版本出现报错#1929 Incorrect datetime value: '''' for column ''createtime''
-
使用Mysql5.x以上版本出现报错#1929 Incorrect datetime value: '''' for column ''createtime''的快速解决方法
-
使用Mysql5.x以上版本出现报错#1929 Incorrect datetime value: '''' for column ''createtime''的快速解决方法
-
mysql now() Incorrect datetime value for column_MySQL
-
mysql now() Incorrect datetime value for column_MySQL
-
使用Mysql5.x以上版本出现报错#1929 Incorrect datetime value: '''' for column ''createtime''
-
在用mybatis向MySQL数据库中插入时间时报错:Incorrect datetime value: '' for column '' at row 1