> I'm new to tg and sqlalchemy, I worte my simple sa model with some
> integrity costraint such as unique=True
>
> If i create a new database object for example
>
> obj=DBObject(name='test',other='other')
>
> and then save
>
> DBSession.add(obj)
>
> the first time all works fine, if I create the same object and try to
> save the unique costraint is violated and my controller return a 500
> error, obviously I try to use:
>
> try:
>    DBSession.add(obj)
> except:
>    pass
>
> but nothing a TypeError: Already committed is raised,

I don't understand what you mean by this last line. Your approach is a
good one, wrap the save in a try/except block. I don't use SA so I
don't know what exception is raised if the unique constraint is
violated, but I'm sure you can try it out in a python shell and see
what this exception is. Let's say it's called
ConstraintViolationException. Then you would do

try:
    DBSession.add(obj)
except ConstraintViolationException:
    # return something that will
    # notify the user that something
    # went wrong


> I need to make a query before saving to check if the unique costraint
> is violated?

Certainly not. The try/except block will take care of this.

> In my opinion a best solution is a way to trap the
> dberror and show an error message to the users
>
> another question:
>
> if I use paster shell I have to issue transaction.commit() to make
> database change, in tg2 controller seems that transaction.commit is
> automatically called so is enough a DBSession.add(obj) is this
> correct?

I don't use tg2 but as far as I know automatic commits are controlled
by an entry in the configuration files. You can set it to have
automatic commits or you can set it to disable this feature.

Cheers,
Daniel


-- 
Psss, psss, put it down! - http://www.cafepress.com/putitdown

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears" 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/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to