I already asked a similar question, but I thought that maybe I could rephrase it or show what I did next to shed light on what is happening here.
I currently have two identical databases, and I tried to solve the problem (on another issue that I saw) as follows:
class BaseTable(db.Model): __tablename__ = 'TableName' col = db.Column(db.Integer) class SubTable1(BaseTable): __bind_key__ = 'bind1' class SubTable2(BaseTable): __bind_key__ = 'bind2'
The problem is that now the last binding is used everywhere, so if I do it somewhere else:
SubTable1.query.filter_by(col=12).all()
He then receives the results from the second database. If I were to switch the locations of the SubTable classes, the results were the same (Edit for clearity: by which I mean that the results are based on what was determined last, if they were to switch, instead, the query is' bind2 'instead of' bind1 ', as it does now). I really don't know what to do, so if you can help in any way, it will be awesome.
Thanks.
EDIT: If this is not possible (or you just know better or even differently), do it, please let me know. If I could do something like two different db objects, that would be nice, I just donโt know how to do this or what the consequences might be.
EDIT 2: After you tackled this watch and watch, I finally came to the conclusion on how to do it.
In __init __. py:
db1 = SQLAlchemy(app) db2 = SQLAlchemy(app)
In models.py:
class Table1(db1.Model): __tablename__ = 'TableName' __bind_key__ = 'bind1' col = db1.Column(db1.Integer) class Table2(db2.Model): __tablename__ = 'TableName' __bind_key__ = 'bind2' col = db2.Column(db2.Integer)
The reason for this nonsense is that the bindings can only be defined once and not changed, and no two table names can be the same, even if the bindings are different. So you need to make 2 instances of MetaData, otherwise SQLAlchemy will get angry. So the problem is the limitation in SQLAlchemy.