<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
        主站蜘蛛池模板: A在线观看免费网站大全| 免费v片在线观看无遮挡| 四虎在线成人免费网站| 免费亚洲视频在线观看| 亚洲人成自拍网站在线观看| 在线毛片片免费观看| 日韩精品免费电影| 久久国产亚洲精品无码| 免费人成视频在线播放| 精品国产免费人成电影在线观看| 亚洲人成影院在线| CAOPORM国产精品视频免费| 在线a毛片免费视频观看| 亚洲一本综合久久| 国产精品免费观看| 亚洲av永久无码精品古装片| 欧洲亚洲综合一区二区三区| 亚州免费一级毛片| 久久亚洲精品国产亚洲老地址| 99久久国产免费-99久久国产免费| 亚洲精品国精品久久99热| 亚洲国产一区二区三区在线观看| 2021在线观看视频精品免费| 亚洲色无码一区二区三区| 美女黄网站人色视频免费| 日韩伦理片电影在线免费观看| 色一情一乱一伦一视频免费看| 在线亚洲午夜理论AV大片| fc2免费人成在线| 7777久久亚洲中文字幕蜜桃| 久久香蕉国产线看免费| 亚洲精品国产品国语在线| 又大又硬又粗又黄的视频免费看| 日韩精品视频免费在线观看| 在线视频网址免费播放| 亚洲综合精品网站在线观看| 国产成人无码免费网站| 亚洲精品国产福利片| 91免费播放人人爽人人快乐| 久久无码av亚洲精品色午夜| 亚洲精品无码久久久久AV麻豆|