Flask-SQLAlchemy database engine returns table names, but the table keys in metadata are empty

倾然丶 夕夏残阳落幕 提交于 2021-02-07 09:25:24

问题


I am connected to a MS SQL Server. The following returns all the table names in the database:

app.config.from_object('config')
db = SQLAlchemy(app)

db.engine.table_names()

However, this doesn't:

db.metadata.tables.keys() // returns: dict_keys([])

Similarly, this doesn't work:

db.table('dbo.users').primary_key // returns: ColumnSet([])

However, I am able to execute SQL queries. What would be the problem?


回答1:


Engine.table_names gives you a list of available table names from the database. Metadata.tables is a mapping of declared tables associated with the metadata.

If you want to populate the metadata with what's available in the database, use reflection:

db.metadata.reflect(bind=db.engine)


来源:https://stackoverflow.com/questions/40984480/flask-sqlalchemy-database-engine-returns-table-names-but-the-table-keys-in-meta

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!