views:

138

answers:

1

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.

+3  A: 

Something like:

table = User.__table__
field = table.c["fullname"]
print "Type", field.type
print "Length", field.type.length
print "Nullable", field.nullable
codeape
I've been spending so much time with `declarative` that I forgot all about looking at the underlying columns. Thanks!
Adam Batkin