Using different bindings in the same class in Flask-SQLAlchemy

Currently, I have several databases with the same tables and columns (but different data inside). So I have to use bindings to access all of them, but this is apparently not as simple as doing it:

class WhateverTable(db.Model): __tablename__ = 'whatevertable' whatever = db.Column(db.String(255)) def __init__(self, bind=None): self.__bind_key__ = bind 

and then later:

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

Is there a way I can do this easily? I tried to inherit from a table class and then defined the binding there, and although this works, it really does not scale.

EDIT: This: Flask inherited table classes in several identical databases using __bind_key__ does what I want, but I don’t want to have different classes, because if I ever add a new database, I will have to create a new set of classes and relationship.

+4
source share
1 answer

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

+1
source

Source: https://habr.com/ru/post/1485435/


All Articles