akaGrim
akaGrim

Reputation: 337

Check the attributes for a SQLAlchemy Column?

I would like function accept a class that has inherited SQLAlchemy's declarative_base, and it would return a different result depending on the attributes of its columns. Here is a bit of code to illustrate my concept.

def my_func(sql_class):
    result = 0

    for col in sql_class.__table__.colomns:
         if col.type == INTEGER:
             if col.type.unsigned:
                 result += 5
             else:
                 result += 10
         else:
             result += 1

   return result

The problem I'm having is I don't know how to access the attributes of each column. I know there is a '.type' attribute, but I'm unsure how to do a comparison with it. And even then, it does not give me values such as signed or unsigned.

I'd appreciate any help, thanks.

Upvotes: 1

Views: 969

Answers (1)

erilem
erilem

Reputation: 2664

col.type is an instance of a SQLAlchemy type class. See http://docs.sqlalchemy.org/en/rel_0_7/core/types.html#generic-types for the list of types supported by SQLAlchemy.

Upvotes: 2

Related Questions