Michael Bayer wrote: >On Jan 20, 2007, at 2:59 AM, jose wrote: > > > >>I would like to do that using the mapper, but... >>In a PostgreSQL DB I have two tables Anag and User linked by Anag.c.id >>== User.c.anag_id >>Anag.c.id is a serial type, after inserted the Anag row, I want to >>find >>the last id to insert it into the User table. >>I tried this code using the command >>select([func.max(Anag.c.id)]).execute().fetchone()[0] >>to have the last id, but it doesn't returns the last id but the >>previous >>one. >> >> > > >when using the mapper, the primary key of any entity that's been >flushed is available directly off the entity's attributes: > > oops! :-[ you are rigth Mike!
>anag = Anag( > name = 'pippo', > address = 'rue cinc', > ) >session.save(anag) >session.flush() > >user = User( > anag_id = anag.id > ) > >also i would think the func.max() thing works, i dont know what you >mean by "but it doesn't returns the last id but the previous one." > in this context, it doesn't work for me, probably it is my fault. > >though the big issue with using func.max() is that it wont behave >consistently in a concurrent environment, since some other process or >thread might insert into the table before you can get your last id. > > you are rigth Mike, and this was my concern, infact I didn't want to use it, now thanks to your tip I can access anag.id directly after flush(). Thank you, jo --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---