class RoutedSession(Session):
def execute(self, *args, **kwargs):
return super().execute(*args, **kwargs, use_master=True)
def get_bind(self, mapper=None, clause=None, use_master=False):
if use_master or self._flushing:
return ENGINES['master']
return random.choice(ENGINES['slaves'])
model = MyModel()
session.add(model)
session.commit()
print(model.id) # ObjectDeletedError: Instance '<Model at 0x...>' has been deleted, or its row is otherwise not present.
File "tests/mock.py", line 2414, in populate_db_with_mock_data
contact_id=contact2.id, account_id=account.id,
File "sqlalchemy/orm/attributes.py", line 275, in __get__
return self.impl.get(instance_state(instance), dict_)
File "sqlalchemy/orm/attributes.py", line 669, in get
value = state._load_expired(state, passive)
File "sqlalchemy/orm/state.py", line 632, in _load_expired
self.manager.deferred_scalar_loader(self, toload)
File "sqlalchemy/orm/loading.py", line 985, in load_scalar_attributes
raise orm_exc.ObjectDeletedError(state)
> You can adjust `expire_on_commit` if you're only doing short-term read-only actions.Can you expand on this? Or link to docs/blog so I can do some research. Google hasn't helped me so far. Why would I want to expire after every commit?
---I agree with your assessment. I think its because every time I call "session". I'm actually saying "session_maker()". So the _flushing attribute will be reset because its a new session instance.On Thursday, March 26, 2020 at 1:02:18 PM UTC-5, Jonathan Vanasco wrote:My first guess is two things are going on:1. This is a behavior of `expire_on_commit` on the session. Once you commit on the Primary database, the object is stale.2. The session is then trying to read off a Secondary database, but the row has not yet synced.You can adjust `expire_on_commit` if you're only doing short-term read-only actions. However, I would explore to ensure this is trying to read off the other database and why.
--SQLAlchemy -The Python SQL Toolkit and Object Relational MapperTo post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.---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.To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/8185b96d-35cb-4973-a9cf-1e572ebd643b%40googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlal...@googlegroups.com.
Aside from disabling expire_on_commit, any thoughts on how I can prevent this error? I guess I just need a method to force the attribute refresh to use the master database. I'm just not sure where I should put that. Thoughts?
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/dd5c4d05-5af3-451e-b965-52ac50b432ac%40googlegroups.com.
is the issue that your follower database is only updating asynchronously? I would likely organize the application to simply use two different Session objects, one for master one for follower. Trying to do it on a per-query basis is needlessly implicit and complicated.