SQLAlchemy determine whether an Integer attribute is a Foreign Key or not using inspector

Viewed 789

I'm defining a sqlalchemy model like this:

class Dog(Model):
    __tablename__ = 'dog'

    id = db.Column(db.Integer, primary_key=True)
    owner = db.Column(db.Integer, db.ForeignKey('owner.id'))

I want to use the inspector to figure out a type for each attribute, however I'm having trouble figuring out how to access the things I want, which include (a) a type for each attribute and (b) all of the other properties that I've passed into Column.

I tried the following:

for column in inspect(target_class).columns:
    print column.type

This returns:

INTEGER
INTEGER

but really I'd like something like

INTEGER
FOREIGN_KEY

or at least some way to recognize that I'm using the second Integer to identify another table.

What is the most correct way to do this? If possible, I'm also interested in grabbing all of the kwargs that I passed into db.Column.

1 Answers
Related