<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當前位置: 首頁 - 科技 - 知識百科 - 正文

        SettingMySQLDATETIMEcolumndefaultsinRails_MySQL

        來源:懂視網 責編:小采 時間:2020-11-09 19:17:44
        文檔

        SettingMySQLDATETIMEcolumndefaultsinRails_MySQL

        SettingMySQLDATETIMEcolumndefaultsinRails_MySQL: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 dont need to remem
        推薦度:
        導讀SettingMySQLDATETIMEcolumndefaultsinRails_MySQL: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 dont need to remem

        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 genericcolumndefinition 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.

        聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        SettingMySQLDATETIMEcolumndefaultsinRails_MySQL

        SettingMySQLDATETIMEcolumndefaultsinRails_MySQL: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 dont need to remem
        推薦度:
        標簽: co mysql dat
        • 熱門焦點

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 在线看片免费人成视频久网下载| 77777_亚洲午夜久久多人| 亚洲欧洲日本在线观看| 3344永久在线观看视频免费首页| 成人a毛片视频免费看| a毛片免费播放全部完整| 亚洲综合无码AV一区二区| 亚洲第一视频在线观看免费| 国产亚洲日韩一区二区三区| 成人免费网站视频www| 相泽亚洲一区中文字幕| 99久久精品毛片免费播放| 国产伦精品一区二区三区免费下载 | 亚洲男人电影天堂| 国产成人免费ā片在线观看老同学| 亚洲熟伦熟女新五十路熟妇| 一区二区三区在线免费| 国产成A人亚洲精V品无码| 玖玖在线免费视频| 亚洲欧洲尹人香蕉综合| 羞羞漫画在线成人漫画阅读免费| 又粗又硬免费毛片| 最新久久免费视频| 亚洲欧洲国产视频| 免费国产综合视频在线看| 国产免费内射又粗又爽密桃视频 | 久久亚洲国产成人影院| 国产v精品成人免费视频400条| 免费欧洲美女牲交视频| 免费福利在线观看| 亚洲图片在线观看| 日本免费一区二区三区最新vr| www成人免费观看网站| 久久精品国产亚洲AV香蕉| 妞干网手机免费视频| 国产免费人成视频尤勿视频 | 亚洲狠狠狠一区二区三区| av无码国产在线看免费网站| 亚洲日韩精品国产一区二区三区| 6080午夜一级毛片免费看| 亚洲av永久无码一区二区三区|