On Oct 27, 2011, at 9:19 AM, Vlad K. wrote: > I have apps that solve the same problem and I rely on IntegrityError. I > simply catch it and return the information to the user that the username is > already taken. Someone else here suggested you should check if the username > exists first, but that is not as good a solution. What if another process > registers that username between your check and commit? While this may seem > unlikely, depending on the rate of new user generation, it doesn't make sense > to do that if the naturally transactional and robust way is to rely on > IntegrityError for no extra code required, especially since you can't lock > unexisting rows for update: > > try: > session.add(user) > except IntegrityError as e: > transaction.abort() > if "user_name_key" in e: > print "The username already exists" > # Or whatever > elif "email_key" in e: > print "The email aready exists." > # Or whatever
Vlad, Thanks for the reply. That's the way I was leaning. I just wondered if there was a better place to catch the exception within the Pyramid framework. Handlingly it locally in the page processing code makes the most sense as that's where the best knowledge of how to handle it exists. In my code, it seems like the IntegrityError is actually raised by a call to transaction.commit(), not the call to session.add(). Does your code have some sort of automatic commit going on? Thanks again, Mark -- You received this message because you are subscribed to the Google Groups "pylons-discuss" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en.
