Thanks Michael for your time. Yes, I am using scope session to create 
Session. I want that all types of write operations *(insert, delete, 
update)* should run on write engine and only read operations will perform 
on read engine.

class SQLAlchemySessionRouter(Session):
def get_bind(self, mapper=None, clause=None):
if self._flushing:
return ENGINES['write-only']
else:
return ENGINES['read-only']

So the problem with above mentioned class is that when I run any update or 
delete query like query.update() and query.delete() then it uses read 
engine instead of write engine.

So to fulfill my requirement I have modified above class like this.

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']

I need your opinion Michael, Are these modification good? Should I go with 
them? *Or is there any better way to do this?*

Looking forward to your thoughts.

On Sunday, February 15, 2015 at 1:11:53 AM UTC+5, Michael Bayer wrote:
>
>
>
> ahmadj...@gmail.com <javascript:> 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 sqlalchemy+...@googlegroups.com <javascript:>. 
> > To post to this group, send email to sqlal...@googlegroups.com 
> <javascript:>. 
> > 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 sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to