On Mon, Jan 12, 2009 at 7:09 AM, drakkan <[email protected]> wrote: > > > > On 12 Gen, 11:31, "Jorge Vargas" <[email protected]> wrote: >> On Mon, Jan 12, 2009 at 2:36 AM, drakkan <[email protected]> wrote: >> >> > Thaks for your answers but maybe I haven't well explained the problem >> >> > Please look at this examples: >> >> > paster shell >> >> > from sambadm.model.smbserver import * >> > import transaction >> >> > s=SambaShares(nome='testunique',path='/tmp/testunique') >> > DBSession.add(s) >> >> > try: >> > transaction.commit() >> > except: >> > print 'error' >> >> to start with you should never use except without the exception type, >> your current code will catch even the interpreter running out of >> memory (maybe not so far) but you should always catch the relevant >> error not a generic one, that may be the source of your issue. I'm >> almost certain that you "web request error" is that your eating up >> TurboGears internal 404 handling which uses an exception. >> >> as for the pattern you are using it makes little sense why will you >> explicitly violate the constraint? I don't get it. > > the users can give any values even if these values violate unique > costraints, and validation server side is better than client side (for > example via javascript) > That is not correct. The earlier you catch the error the better. That said in this particular case model validation is optiomal.
>> >> As for your last snip, the pattern to follow is, "ask for forgiveness >> rather than permission". but your code is weird. the idiom I normally >> use is: >> instance = DBSession.query(...).one() or first() >> if instance: >> return dict(instance=instance) >> else: >> return dict(error='no records found') >> >> you "add to the session" makes no sense there because you are saying >> if the object you pulled from the db (and stored in existe) is not in >> the db store it in the db. > > my add to session make sense since it says: if in the db there is > already an object with the parameter 'nome' the user ask to add > (violating unique costraint) return an error, else insert the > requested object in the db. no your doing it wrong. You need to create an object, then try to save it. Either you want to do a "select or insert" they are two different use cases your mixing. Take a look at a select example here http://turbogears.org/2.0/docs/main/Wiki20/wiki20.html#hey-where-s-the-page if you want "insert" you will need something of the following nature. try: instance = model.Type(....) DBsession.add(instance) transaction.commit() except IntegrityError: return dict(error='You have a duplicated constraint') I'm not 100% sure of the IntegrityError, I think that moved around in 0.4/0.5 but all you need to do is run it with out and figure out which exception class it's using. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---

