SQLAlchemy如何给列和表添加注释comment?

1、首先需要升级版本到1.2.x,我用的是1.2.14验证的,没有问题

2、看示例:

class LoadResource(Base):
    """施压机资源."""
    # def __init__(self):
    #     pass
    __tablename__ = 'load_resource'   # 数据库名字

    id = Column(Integer, primary_key=True, doc='主键', comment='主键')  # 压测资源主键,comment为列注释
    resource_ip = Column(String(50), doc='资源ip', comment='资源ip')        # 压测资源IP
    resource_zone = Column(String(50), doc='资源区域', comment='资源区域')      # 压测资源区域
    project_id = Column(String(250), doc='项目id', comment='项目id')      # 占用资源的项目ID
    scene_id = Column(String(250), doc='场景id', comment='场景id')        # 占用资源的场景ID
    # task_name = Column(String(250))         # 占用资源的任务名字
    task_uid = Column(String(250), doc='任务id', comment='任务id')          # 占用资源的任务ID,根据任务ID可以得知项目和场景信息
    resource_status = Column(Boolean, default=False, doc='资源状态', comment='资源状态')                                # 压测资源可用状态,默认False表示可用
    gmt_create = Column(TIMESTAMP, server_default=func.now(), doc='创建时间', comment='创建时间')                       # 创建时间
    gmt_modify = Column(TIMESTAMP, server_default=func.now(), onupdate=func.now(), doc='更新时间', comment='更新时间')  # 修改时间
    __table_args__ = (Index('index(zone,status)', 'resource_zone', 'resource_status'), {'comment': '压测资源表'})  # 添加索引和表注释

下面添加索引的方法也可以

class A(Base):
    __tablename__ = 'table_A'
    id = Column(Integer, primary_key=True)
    a = Column(String(32))
    b = Column(String(32))

Index('my_index', A.a, A.b)

参考:

https://docs.sqlalchemy.org/en/latest/core/metadata.html

原文地址:https://www.cnblogs.com/shengulong/p/9989618.html