On May 8, 2007, at 5:45 PM, noah.gift wrote:

>
>
>
> On May 8, 1:58 pm, Michael Bayer <[EMAIL PROTECTED]> wrote:
>> On May 8, 2007, at 1:23 PM, noah.gift wrote:
>>
>>
>>
>>
>>
>>> I am trying to use the following code to handle an exception in
>>> Turbogears, but it does not grab the SQLError:
>>
>>> please note I did a:
>>> from sqlalchemy.exceptions import SQLError
>>
>>> def save(self, name=None, email=None, password=None,
>>> password_confirm=None,
>>>              **kw):
>>>         try:
>>>             u = User(user_name=name, display_name=name, email=email,
>>> password=password)
>>>             raise redirect("/registered")
>>
>>>         except SQLError:
>>>             flash("That account already exists.")
>>
>> i think youre confusing a pattern from SQLObject here.  creating a
>> new object doesnt write any changes to the database in sqlalchemy.
>
> Your right.  I wasn't sure of how much magic was occuring with
> Turbogears, but I really do like that I have to be explicit even
> inside of Turbogears.
> Forgive the dumb question, but what is the proper SQLAlchemy
> recommended way to deal with a situation like this.
> Should I see if the object already exists first, or be lazy and try to
> write to the database and attempt to catch the exception.
> I love to read, so if you can point me to some examples on situations
> like this it would be very helpful.

Well, my style would be to just query the database first, then if row  
exists just report to the user.  since if you wait for an exception,  
you have to inspect the exception itself to check that its the "key  
already exists" error...and those errors will be different depending  
on the particular database in use.   Unfortuantely DBAPI doesnt  
specify any standardized way to get at error codes and SA doesnt have  
any kind of "error categorizing" logic...so its a little bit of a  
guessing game.

also you might want to have several operations related to creating a  
new user all within a single transaction which means catching the  
exception also means you have to roll back the transaction, making it  
more inconvenient to use exception catching as a "check if the user  
exists" type of function.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to