I've already asked a similar question, but I thought maybe I could rephrase it, or show what I've done further to shed some light onto what's going on here.
Currently I have 2 identical databases, and I've attempted to solve the problem (as per another question I saw) like this:
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 with this is that now the most recent bind is used everywhere, so if I do this somewhere else:
SubTable1.query.filter_by(col=12).all()
Then it gets results from the second database. If I were to switch the locations of the SubTable classes, then the results are the same (Edit for clarity: by which I mean that the results come from whatever bind is defined last, if they were to be switched, it would instead query from 'bind2' instead of 'bind1' as it currently does). I don't really know what to do, so if you can help in any way that would be awesome.
Thanks.
EDIT: If it's impossible (or you simply know a better or even different way) to do this, please let me know. If I could do something like having two different db objects, that would be good as well, I just don't really know how to do that or what kind of implications that would have.
EDIT 2: After toiling with this for hours and hours, I've finally come to a conclusion on how to do this.
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 binds can only be defined once and not changed, and no two table names can be the same, even if the binds are different. So you have to make 2 MetaData instances or else SQLAlchemy gets mad. So it turns out the problem is a limitation in SQLAlchemy.