This one time, at band camp, Jamie Wilkinson wrote: >This one time, at band camp, Michael Bayer wrote: >>you can hack this with a MapperExtension after_insert rule that sets >>the "account_id" field on the Person object after the "account" table >>is inserted. a little ugly but probably would work right now. > >I'm having trouble working out where to get the primary keys from; >after_insert gets called at the end of the account table insert, adn then >after the person table insert; relatively easy to test which table's being >inserted, but I can't get the primary key of the account row, it's not kept >in the mapper during that loop and no identifying marks are passed to >after_insert either. > >Any clues would be much appreciated ;-)
Damn, as usual right after posting I got it working ;-) Clagged here for posterity, hopefully others looking for an example on how to use after_insert will find this useful. (it hardcodes table names and column names, so caveat emptor) class NToOneMapperExtension(MapperExtension): def after_insert(self, mapper, connection, instance): for table in mapper.tables: if table.name == 'account': for col in mapper.pks_by_table[table]: account_id = mapper._getattrbycolumninstance, col) break instance.account_id = account_id mapper(Person, join(account, person), extension=NToOneMapperExtension()) ------------------------------------------------------- All the advantages of Linux Managed Hosting--Without the Cost and Risk! Fully trained technicians. The highest number of Red Hat certifications in the hosting industry. Fanatical Support. Click to learn more http://sel.as-us.falkag.net/sel?cmd=lnk&kid=107521&bid=248729&dat=121642 _______________________________________________ Sqlalchemy-users mailing list Sqlalchemy-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users