Note that there are some explanatory texts on larger screens.

plurals
  1. POshould SQLAlchemy session.begin_nested() be committed with transaction.commit() when using pyramid_tm?
    primarykey
    data
    text
    <p>I'm developing a pyramid application and currently in the process of moving from sqlite to postgresql. I've found postgresql more restrictive transaction management is giving me a bad time.</p> <p>I am using the pyramid_tm because I find it convenient. Most of my problems occur during my async calls. What I have is views that serve up dynamic forms. The idea is - if we got an id that corresponds to a database row we edit the existing row. Otherwise, we are adding a new guy.</p> <pre><code>@view_config(route_name='contact_person_form', renderer='../templates/ajax/contact_person_form.pt', permission='view', request_method='POST') def contact_person_form(request): try: contact_person_id = request.params['contact_person'] DBSession.begin_nested() contact_person = DBSession.query(ContactPerson).filter(ContactPerson.id == contact_person_id).one() transaction.commit() except (NoResultFound, DataError): DBSession.rollback() contact_person = ContactPerson(name='', email='', phone='') return dict(contact_person=contact_person) </code></pre> <p>I need to begin a nested transaction because otherwise my lazy request method which is registered with <code>config.add_request_method(get_user, 'user', reify=True)</code> and called when rendering my view</p> <pre><code>def get_user(request): userid = unauthenticated_userid(request) if userid is not None: user = DBSession.query(Employee).filter(Employee.id == userid).first() return user </code></pre> <p>complains that the transaction has been interrupted and the SELECT on employees will be skipped.</p> <p>I have two questions:</p> <ol> <li>Is it okay to do <code>transaction.commit()</code> on a <code>session.begin_nested()</code> nested transaction? I don't exactly where SQLAlchemy ends and pyramid_tm begins. If I try to commit the session I get an exception that says I can only commit using the transaction manager. On the other hand DBSession.rollback() works fine.</li> <li><p>Does handling this like</p> <pre><code>try: #do something with db except: #oops, let's do something else </code></pre></li> </ol> <p>make sense? I have the feeling this is 'pythonic', but I'm not sure if this underlying transaction thing calls for non-pythonic means.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload