Adrian, Adrian von Bidder wrote: > Hi, > > Is it possible to fetch the values of an autoincrement field without > flushing the object to the DB? > > (In postgres, I obviously can manually fetch nextval of the automatically > generated sequence, but I lose the portability that way ...) > > Why? > > Because I need the id to generate data that will be filled into some (non- > null) columns of the table row. So I can't flush since I'll get an > IntegrityError about non-null columns, and I can't fill those columns > without knowing the id that's going to be assigned. > > (Yes, I can use dummy values, then flush(), and then update the row before > committing. But that's not exactly elegant...) > This is one of the beauties of SQLA which it took me a while to catch on to.
I use SQLA ORM (declarative) and you can just do: lang = session.query(db.Language).get(1) ca = db.Country_Ls() ca.language = lang ca.name = 'some country' reg = db.Region_Ls() reg.language = lang reg.name = 'some region' reg.country_ls = ca session.add(ca) session.add(reg) print ca print reg session.flush() print '=======' print 'flushed' print '=======' print ca print ca.countryid print '--------' print 'region' print reg print reg.fk_countryid Which gives me this output and as you can see the actual primary key and foreign key are only known after I do a flush, but it is not needed to add etc. Country_Ls(language=Language(created=datetime.date(2009, 5, 22), langid=1, locales=u'en ', name=u'English', updated=datetime.date(2009, 5, 22)), name='some country') Region_Ls(country_ls=Country_Ls(language=Language(created=datetime.date(2009, 5, 22), langid=1, locales=u'en ', name=u'English', updated=datetime.date(2009, 5, 22)), name='some country'), language=Language(created=datetime.date(2009, 5, 22), langid=1, locales=u'en ', name=u'English', updated=datetime.date(2009, 5, 22)), name='some region') ======= flushed ======= Country_Ls(centralkey=None, countryid=241, created=datetime.datetime(2009, 5, 22, 13, 22, 48, 826000), fk_langid=1, id=None, language=Language(created=datetime.date(2009, 5, 22), langid=1, locales=u'en ', name=u'English', updated=datetime.date(2009, 5, 22)), name='some country', shortname=None, updated=datetime.datetime(2009, 5, 22, 13, 22, 48, 826000)) 241 -------- region Region_Ls(centralkey=None, country_ls=Country_Ls(centralkey=None, countryid=241, created=datetime.datetime(2009, 5, 22, 13, 22, 48, 826000), fk_langid=1, id=None, language=Language(created=datetime.date(2009, 5, 22), langid=1, locales=u'en ', name=u'English', updated=datetime.date(2009, 5, 22)), name='some country', shortname=None, updated=datetime.datetime(2009, 5, 22, 13, 22, 48, 826000)), created=datetime.datetime(2009, 5, 22, 13, 22, 48, 829000), fk_countryid=241, fk_langid=1, id=None, language=Language(created=datetime.date(2009, 5, 22), langid=1, locales=u'en ', name=u'English', updated=datetime.date(2009, 5, 22)), name='some region', regionid=214, shortname=None, updated=datetime.datetime(2009, 5, 22, 13, 22, 48, 829000)) 241 Hope this helps Werner --~--~---------~--~----~------------~-------~--~----~ 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 sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~----------~----~----~----~------~----~------~--~---