美文网首页
create_at & update_at

create_at & update_at

作者: lindyang | 来源:发表于2021-04-07 10:10 被阅读0次

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)

相关文章

网友评论

      本文标题:create_at & update_at

      本文链接:https://www.haomeiwen.com/subject/ltiykltx.html