繁体   English   中英

Flask-SqlAlchemy查看反射

[英]Flask-SqlAlchemy views reflection

SQLAlchemy类的方法有:

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

    Reflects tables from the database.

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

原生SqlAlchemy的方法反射有更多的参数和视图= False是其中一个允许视图反射,如果设置为True。

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

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

对SQLAlchemy类进行子类化并覆盖该函数。 这是Flask领域中可接受的自定义方法。 Flask文档本身讨论了对Flask类进行子类化以获得所需内容。

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

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM