On Sun, Jan 11, 2009 at 4:23 PM, Diez B. Roggisch <[email protected]> wrote: > >>> I need to make a query before saving to check if the unique costraint >>> is violated? In my opinion a best solution is a way to trap the >>> dberror and show an error message to the users >>> >> no you need to "flush to the db" in tg2 this is done by issuing >> transaction.commit(), which together with other things calls >> DBSession.flush() > > This is not correct. At least the "no you need" (which I presume you > mean "you don't need") part. Because of the lazyness of SQLAlchemy, a > mapped object won't get an ID until the next flush. As you correctly > state, this is eventually done implicitly by the commit. > actually it was supposed to read 'no, you need to "flush to the db" ', that will trigger the validation of the unique constraint, I'm not sure why you think the id is involved. The first question "I need to make a query...." is a common misconception, of the lazyness of SA. In other words, you need to call transaction.commit() to get the object into the db (flush), but you can alternatively get this done, by issuing a query which causes the session to flush, in order to keep data consistent.
> But often enough when you create an object, you want to e.g. render a > page containing it's ID - and thus you need to call flush. > > Diez > > > > --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---

