【问题标题】:Flask-SqlAlchemy views reflectionFlask-SqlAlchemy 视图反射
【发布时间】:2014-04-25 12:17:47
【问题描述】:

SQLAlchemy 类有方法 reflect:

 reflect(bind='__all__', app=None)

    Reflects tables from the database.

它只有 2 个参数:bind 和 app。我在metadata.tables 中找不到任何视图。

原生 SqlAlchemy 的方法 reflect 有更多参数,views=False 是其中之一,如果设置为 True,则允许 Views 反射。

reflect(bind=None, schema=None, views=False, only=None, extend_existing=False, autoload_replace=True, **dialect_kwargs)

是否可以在 Flask-SqlAlchemy 中以某种方式反映数据库视图?

【问题讨论】:

标签: python flask sqlalchemy flask-sqlalchemy


【解决方案1】:

子类化 SQLAlchemy 类并覆盖该函数。这是 Flask 领域中公认的自定义方法。 Flask 文档自己谈论继承 Flask 类以获得您需要的东西。

这未经测试,但这是一个开始。它允许将 kwargs 传递给执行和反射,将它们传递给真正的操作。

class MySQLAlchemy(SQLAlchemy):
    def _execute_for_all_tables(self, app, bind, operation, **kwargs):
        app = self.get_app(app)

        if bind == '__all__':
            binds = [None] + list(app.config.get('SQLALCHEMY_BINDS') or ())
        elif isinstance(bind, basestring) or bind is None:
            binds = [bind]
        else:
            binds = bind

        for bind in binds:
            tables = self.get_tables_for_bind(bind)
            op = getattr(self.Model.metadata, operation)
            op(bind=self.get_engine(app, bind), tables=tables, **kwargs)

    def reflect(self, bind='__all__', app=None, **kwargs):
        self._execute_for_all_tables(app, bind, 'reflect', **kwargs)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-28
    • 1970-01-01
    • 2022-01-15
    • 2013-06-20
    • 1970-01-01
    • 2021-11-25
    相关资源
    最近更新 更多