问题
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