if the versions are truly just 1-N based, why even call to the database to get the number ? if you set up a backreference on Version to get its Project, you can just do: class VersionKeyMapper(MapperExtension): def before_insert(self, mapper, connection, instance): instance.id = instance.project.versions.index(instance) + 1 or you can just modify Project to assign numbers as versions are appended, that way you have the numbers before you even have to flush(): class Project(object): def add_version(self, version): version.id = len(self.versions) + 1 self.versions.append(version) or if you want the totally clean API, do the "custom list class" thing: class VersionList(list): def append(self, item): if getattr(item, 'id', None) is None: item.id = len(self) + 1 list.append(self, item) class Project(object): versions = VersionList i was thinking you could also do it via a column default combined with a thread local, but id have to modify the API a little bit to get the current Connection in there. the mapper's save_obj is written the way it is to support batching. which it doesnt do at the moment because it makes the "concurrency" check impossible with DBAPI's current behavior for executemany(), and also theres other stuff regarding defauls that wouldnt work right now (but could work with a little tweaking). but if we flip it inside out then theres no chance of ever supporting that, or we'd have to have two totally different versions of save_obj...although if you have some insight on how to do it in a reasonably readable fashion im open to that (since i suppose this sort of thing is going to be needed by others as well). On Sep 21, 2006, at 3:45 PM, Hogarty, David A. wrote:
|
------------------------------------------------------------------------- Take Surveys. Earn Cash. Influence the Future of IT Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys -- and earn cash http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________ Sqlalchemy-users mailing list Sqlalchemy-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users