SQLAlchemy - Introspection of ORM classes / objects

I am looking for a way to examine SQLAlchemy ORM classes / entities to determine the types and other restrictions (e.g. maximum length) of entity 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 like to know that the " fullname" field must be a string with a maximum length of 100 and is NULL. And the field ' created_timestamp' is a DateTime and is not NULL.

+5
source share
1 answer

Sort of:

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

EDIT:

The next version 0.8 has a new class verification system :

New Class Inspection System

Status: completed, documents required

SQLAlchemy , , , , .., , , JSON/XML .

Table Column , . SQLAlchemy ORM , , , , , .

0.8 , API , , , , , . , , API, , Mapper, InstanceState MapperProperty:

( )

+11

All Articles