Reputation: 773
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
.
Upvotes: 3
Views: 881
Reputation: 20548
You can look at the foreign_keys
property on Column
:
for column in inspect(Dog).columns:
print column.foreign_keys
# set([])
# set([ForeignKey('owner.id')]
There are other properties set from kwargs, like primary_key
:
for column in inspect(Dog).columns:
print column.primary_key
# True
# False
Upvotes: 2