Using different binds in the same class in Flask-SQLAlchemy

末鹿安然 提交于 2019-12-06 05:19:10

问题


I currently have multiple databases with identical Tables and Columns (but different data inside). So clearly I need to use binds to access all of them, but it's apparently not as simple as doing this:

class WhateverTable(db.Model):
    __tablename__ = 'whatevertable'
    whatever = db.Column(db.String(255))

    def __init__(self, bind=None):
        self.__bind_key__ = bind

and then later calling:

WhateverTable(bind='bind_key_here').query.filter_by(whatever='whatever').first()

Is there a way I can do this easily? I've tried inheriting from a table class and then defining the bind there, and while that works, it really isn't scalable.

EDIT: This :Flask inherited classes of tables in multiple identical databases using __bind_key__ does sort of what I want, but I don't want to have different classes because if I ever add a new database, I'm going to have to create a new set of classes and relationships.


回答1:


Flask-SQLAlchemy 2.1 added support for a binds parameter on the session which should do what you want.



来源:https://stackoverflow.com/questions/17024850/using-different-binds-in-the-same-class-in-flask-sqlalchemy

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