Welcome to JiKe DevOps Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
339 views
in Technique[技术] by (71.8m points)

python - How to get table names and their data types from session/engine/meta in sqlalchemy?

Suppose I have a Model:

class User(Base):
    __tablename__ = 'users'
    id = Column(Integer, Sequence('user_id_seq'), primary_key=True)
    name = Column(String(50))
    fullname = Column(String(50))
    nickname = Column(String(50))

Now in code, I want all the data types of the User class, what is the best possible way of doing that?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

Please log in or register to answer this question.

1 Answer

0 votes
by (71.8m points)

You can access the __table__ attribute of your model and interact with the column elements directly:

for for x in User.__table__.c:
    print(x, x.type)
>> users.id INTEGER
>> users.name VARCHAR(50)
>> users.fullname VARCHAR(50)
>> users.nickname VARCHAR(50)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to JiKe DevOps Community for programmer and developer-Open, Learning and Share
...