SQLAlchemy ORM 类/对象的自省
我正在寻找一种方法来内省 SQLAlchemy ORM 类/实体,以确定实体属性的类型和其他约束(如最大长度)。
例如,如果我有一个声明性类:
class User(Base):
__tablename__ = "USER_TABLE"
id = sa.Column(sa.types.Integer, primary_key=True)
fullname = sa.Column(sa.types.String(100))
username = sa.Column(sa.types.String(20), nullable=False)
password = sa.Column(sa.types.String(20), nullable=False)
created_timestamp = sa.Column(sa.types.DateTime, nullable=False)
我希望能够发现 'fullname
' 字段应该是一个最大长度为 100 的字符串,并且可以为空。 'created_timestamp
' 字段是一个 DateTime 并且不能为空。
I am looking for a way to introspect SQLAlchemy ORM classes/entities to determine the types and other constraints (like maximum lengths) of an entity's properties.
For example, if I have a declarative class:
class User(Base):
__tablename__ = "USER_TABLE"
id = sa.Column(sa.types.Integer, primary_key=True)
fullname = sa.Column(sa.types.String(100))
username = sa.Column(sa.types.String(20), nullable=False)
password = sa.Column(sa.types.String(20), nullable=False)
created_timestamp = sa.Column(sa.types.DateTime, nullable=False)
I would want to be able to find out that the 'fullname
' field should be a String with a maximum length of 100, and is nullable. And the 'created_timestamp
' field is a DateTime and is not nullable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
类似于:
编辑:
即将推出的 0.8 版本有一个 新类检验系统:
点击链接了解更多信息)
Something like:
EDIT:
The upcoming 0.8 version has a New Class Inspection System:
(follow the link for more info)