Documentation for how to setup the mapping isn't particularly helpful about best practice for splitting existing apps across two databases. I spent quite some time playing with multiple DeclarativeBase classes, and eventually just settled on model.init_model( *engines ) creating a mapping from Declarative classes and Tables to the connection which should be used for the table by using introspection on the classes themselves. Once you have the mapping, you pass the mapping into DBSession.configure( binds = your_mapping ).
Spent quite a bit of time trying to get a many-to-many relationship working across the barrier. It would silently "succeed" when I added objects to the relationship, but the objects would be gone when accessed a few seconds later (this test passed on both the single-database version and the properties-and-queries multi-database version).
I'm guessing part of the problem was that the binds weren't passed into the metadata.create_all() call, which was causing the full schema to be created on each connection (since fixed, though I haven't re-tested the M2M because I ripped it out already). It took me quite a bit of time to realize that the secondary table in the M2M relation was actually being populated in the "main" instead of the "stats" DB (I'd overlooked Tables and only mapped Declarative sub-classes in my model init).
So, as of now, I've got a single session, all the tables are mapped, and everything seems to think it is running fine. Thing is, none of the other cross-db relations (even the One to Many) seem to actually be doing anything either, that is, they raise no errors, but the collections they should be retrieving are all empty (this is only the case for cross-db relations). I'm guessing this is going to be a similar problem to the M2M one, but I should have been in bed hours ago, so I won't figure it out tonight.
It is dangerous to play with magic when you have other things to get done. I think I'll abandon this experiment (for this project) and go back to my stupid-simple properties-and-queries solution for the cross-DB connections. Playing with magic can wait for some day when I can afford to spelunk through the tomes of lore at leisure.
Pingbacks are closed.