如何在Flask-SQLAlchemy中创建生成的列?

时间:2019-04-17 20:18:32

标签: python flask-sqlalchemy

此SQL没问题

CREATE TABLE `calendar` (
  `dt` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `d` date as (dt),
  `t` time as (dt)
);

如何在Flask-SQLAlchemy中创建此表?

class Calendar(db.Model):
    __table_args__ = {'mysql_collate': 'utf8_general_ci', 'mysql_engine':'InnoDB'}

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    dt = db.Column(db.DateTime, nullable=False, server_default=db.func.current_timestamp())
    d = ???
    t = ???

db.create_all()

这是错误

class Calendar(db.Model):
    __table_args__ = {'mysql_collate': 'utf8_general_ci', 'mysql_engine':'InnoDB'}

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    dt = db.Column(db.DateTime, nullable=False, server_default=db.func.current_timestamp())
    d = db.ColumnProperty(db.Date,dt)
    t = db.ColumnProperty(db.Time,dt)

2 个答案:

答案 0 :(得分:0)

也许这不是您真正想要的,但仍然:我建议改用function getGuaranteed(id: string): HTMLElement { const el = document.getElementById(id); if (el == null) { throw new Error("Element #" + id + " not found."); } return el as HTMLElement; } see documentation here),就像这样:

hybrid_property

答案 1 :(得分:0)

这对我有用

geom 是引用的列
expire_on_flush 避免在计算列中插入或更新。

distance = column_property(db.Column(db.Float,Computed("Round((st_length(geom,false)::numeric /1000),2)",True),nullable=True),expire_on_flush= True)

希望它有效