create_at & update_at

2021-04-07  本文已影响0人  lindyang

mysql

CREATE_INSERT_TRIGGER = """
CREATE TRIGGER tr_b_ins_{0} BEFORE INSERT ON {0} FOR EACH ROW
BEGIN
  IF (NEW.create_at IS NULL)
  THEN
    SET NEW.create_at = UNIX_TIMESTAMP();
  END IF;
  IF (NEW.update_at IS NULL)
  THEN
    SET NEW.update_at = UNIX_TIMESTAMP();
  END IF;
END
"""

CREATE_UPDATE_TRIGGER = """
CREATE TRIGGER tr_b_upd_{0} BEFORE UPDATE ON {0} FOR EACH ROW
BEGIN
  IF (NEW.update_at IS NULL)
  THEN
    SET NEW.update_at = UNIX_TIMESTAMP();
  END IF;
END
"""

postgres

ts = text('extract(epoch from now())::int')

Column('create_at', Integer, server_default=ts),
Column('update_at', Integer, server_default=ts, server_onupdate=ts)

sqlite

ts = sa.text("(cast(strftime('%s', 'now') as int))")

Column('create_at', Integer, server_default=ts),
Column('update_at', Integer, server_default=ts, server_onupdate=ts)
上一篇 下一篇

猜你喜欢

热点阅读