I'm trying to build tests for flask-sqlalchemy models to confirm they match the tables in the database. Using reflection, I can get the db tables, columns, and foreign keys. With the models I know how to check tablename and column names, but I can't figure out how to see the defined foreign key relationships programatically.
Model example:
class Example(db.Model):
__table_args__ = {'schema': 'defined_schema'}
__tablename__ = 'example'
id = db.Column(db.BigInteger, primary_key=True)
name = db.Column(db.String)
info = db.Column(db.BigInteger, db.ForeignKey(Info.id))
descr = db.Column(db.String)
Given a Model m, I've tried:
dir(m)
m.__dict__
使用sqlalchemy.inspect(model).columns.foreign_keys
和一些字符串操作,我能够将模型外键关系与数据库中的关系进行比较
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.