SQLAlchemy ORM 类/对象的自省

发布于 2024-08-31 19:04:43 字数 585 浏览 2 评论 0原文

我正在寻找一种方法来内省 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

美煞众生 2024-09-07 19:04:43

类似于:

table = User.__table__
field = table.c["fullname"]
print "Type", field.type
print "Length", field.type.length
print "Nullable", field.nullable

编辑:

即将推出的 0.8 版本有一个 新类检验系统

新的班级检查系统

状态:已完成,需要文档

许多 SQLAlchemy 用户正在编写需要这种能力的系统
检查映射类的属性,包括能够
获取主键列、对象关系、简单
属性等,通常用于构建
数据编组系统,例如 JSON/XML 转换方案等
课程形式库丰富。

原来Table和Column模型是最初检查的
点,有一个记录良好的系统。而 SQLAlchemy ORM
模型也是完全可自省的,这从来都不是一个完全的
稳定且受支持的功能,而用户往往没有明确的
了解如何获取此信息。

0.8 计划为此目的生成一致、稳定且完整记录的 API,这将提供一个检查系统
适用于类、实例,也可能适用于其他事物。尽管
该系统的许多要素已经可用,计划是
锁定 API,包括可从中获取的各种访问器
Mapper、InstanceState 和 MapperProperty 等对象:

点击链接了解更多信息)

Something like:

table = User.__table__
field = table.c["fullname"]
print "Type", field.type
print "Length", field.type.length
print "Nullable", field.nullable

EDIT:

The upcoming 0.8 version has a New Class Inspection System:

New Class Inspection System

Status: completed, needs docs

Lots of SQLAlchemy users are writing systems that require the ability
to inspect the attributes of a mapped class, including being able to
get at the primary key columns, object relationships, plain
attributes, and so forth, typically for the purpose of building
data-marshalling systems, like JSON/XML conversion schemes and of
course form libraries galore.

Originally, the Table and Column model were the original inspection
points, which have a well-documented system. While SQLAlchemy ORM
models are also fully introspectable, this has never been a fully
stable and supported feature, and users tended to not have a clear
idea how to get at this information.

0.8 has a plan to produce a consistent, stable and fully documented API for this purpose, which would provide an inspection system that
works on classes, instances, and possibly other things as well. While
many elements of this system are already available, the plan is to
lock down the API including various accessors available from such
objects as Mapper, InstanceState, and MapperProperty:

(follow the link for more info)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文