0

Seems tansaction.commit() is the only way I have to make a commit in pyramid paster pshell. I understand it's good in serving webpages but in shell, after that, on next SQLAlchemy MyModel.my_attribute call I get:

DetachedInstanceError: Parent instance <MyModel at 0x9394d0c> is not bound to
a Session; lazy load operation of attribute 'my_attribute' cannot proceed

How can I avoid it?

neurino
  • 11,500
  • 2
  • 40
  • 63

2 Answers2

1

Pass keep_session=True to ZopeTransactionExtension()

Source: https://pypi.python.org/pypi/zope.sqlalchemy#long-lasting-session-scopes

Michael Platings
  • 3,045
  • 2
  • 25
  • 17
  • Wow, 2 years later, I don't even have the chance to test it anymore, good to know for future reference. – neurino Oct 10 '13 at 08:59
0

I believe this is due to the expire_on_commit option to the SQLA session. Once you perform a commit, the objects you were using with the old transaction must be refreshed or merged into the new session. The point is that this is not actually related to the transaction module itself.

Michael Merickel
  • 23,153
  • 3
  • 54
  • 70
  • 1
    This did not happen (I'm quite sure but can't check now) in Pylons' `paster shell` where I could use `Session.commit()` then use objects again with no `DetachedInstanceError`. Now I cannot use it anymore and I'm invited to use `transaction.commit()`, moreover I should pass `expire_on_commit` to `sessionmaker` which is the one in my `models.py` as `sessionmaker(extension=ZopeTransactionExtension())` and changing it would reflect on served pages too. So, what can I do (beside not using `transaction` anymore)? – neurino Jul 08 '11 at 14:41
  • moreover adding `expire_on_commit = False` to `sessionmaker` does not change things: in `pshell` since, after `transaction.commit()`, I get `DetachedInstanceError` anyway... sob – neurino Jul 11 '11 at 14:35