I've been following the pattern of (in Pyramid webapps) doing DBSession.add 
then letting the web framework flush/commit the DBSession at the end of the 
web request rather than doing it explicitly.

Are there cases where doing it this way could cause unexpected results, for 
example let's say you want to register a user:

DBSession.add(new_user)
 
request.session.flash('new user registered')

headers = remember(request, username) 
return HTTPFound(location=redirect_url, headers=headers)


If at the end of the request the framework goes to commit the change and 
there's some problem, like a constraint violation, then the user wouldn't 
get created. But by that point the flash message has already been created 
and the remember() method is already called to log in the user. So the user 
would be logged in and see a success message even though the user creation 
operation was not successful.

In an official example, see 
https://github.com/Pylons/shootout/blob/master/shootout/views.py#L115, they 
do just this, so is that example wrong or have I misunderstood? I would 
expect that in cases where you are depending on a database operation 
succeeding (in order to do subsequent steps like set a success message or 
log in a user) you would need to commit or flush the database session 
explicitly rather than letting the web framework handle it for you.

What's the correct way of doing this? Would I need to run a 
DBSession.commit/flush or something like that right after the DBSession.add?

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