Flask-SqlAlchemy查看反射 [英] Flask-SqlAlchemy views reflection

查看:781
本文介绍了Flask-SqlAlchemy查看反射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SQLAlchemy类的方法反映:
$ b $ pre $ $ $ $ c $ reflect $(bind ='__ all__',app = None)

反映数据库中的表格。

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



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

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

$ p



$ div b











$ b $ >解决方案

子类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)或bind是None:
binds = [bind]
else:
binds = bind

绑定绑定:
tables = self.get_tables_for_bind(bind)
op = getattr(self.Model.metadata,操作)
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)


SQLAlchemy class has the method reflect:

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

    Reflects tables from the database.

It has only 2 arguments: bind and app. I don't find any views in metadata.tables.

Native SqlAlchemy's method reflect has more arguments and views=False is one of them allowing Views reflection if set up to True.

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

Is it possible to somehow reflect database views in Flask-SqlAlchemy?

解决方案

Subclass the SQLAlchemy class and override that function. This is an accepted method of customization in the Flask realm. The Flask docs themselves talk about subclassing the Flask class to get what you need.

This isn't tested, but here's a start. It allows kwargs to be passed to execute and reflect, passing them on to the real 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)

这篇关于Flask-SqlAlchemy查看反射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆