[email protected] wrote:

> Hi,
> 
> I am working on a web site using Python Django and SQLAlchemy ORM. I want to 
> do all read operations in separate database and write operations in separate 
> database. 
> So I tried Mike Bayer's solution 
> (http://techspot.zzzeek.org/2012/01/11/django-style-database-routers-in-sqlalchemy/)
>  but query.update() and query.delete() didn't work properly. I debugged some 
> code and modified SessionRouter class as following:
> 
> class SQLAlchemySessionRouter(Session):
> 
> def get_bind(self, mapper=None, clause=None):
> if self._flushing:
> return ENGINES['write-only']
> elif isinstance(clause, sqlalchemy.sql.expression.Update):
> return ENGINES['write-only']
> elif isinstance(clause, sqlalchemy.sql.expression.Delete):
> return ENGINES['write-only']
> else:
> return ENGINES['read-only']
> 
> Now it is working fine in my case (I tested this manually).
> 
> I need Mike Bayer or experts guidance. Do these modifications looks good? 
> Should I go with this solution? Or is there some thing better that can help 
> me?


Where this can go wrong is if you are using the Session in a transaction, it 
will run all your SELECT queries on the read engine, and the flushes on the 
write engine, but depending on how your replication and transaction isolation 
is set up, you might not see the rows that you just committed.

I think the blog post mentions this, I should probably update it to accommodate 
using transactions appropriately.    if the recipe works for you as is, then 
keep it in place.



> 
> Thanks in advance,
> 
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to