- changed milestone to blue sky
[patch] Fix problems with database default primary keys and MappedCollections
When the keyfunc includes a mapped object's primary key, it is necessary to flush to the database if that key is a calculated default (e.g. from a sequence). e.g.
ob = MyObject()
session.add(ob)
session.flush()
MyCollection.set(ob)
This patch removes the need to explicitly call session.flush()
Index: collections.py
===================================================================
--- collections.py (revision 4906)
+++ collections.py (working copy)
@@ -1372,6 +1372,10 @@
"""Add an item by value, consulting the keyfunc for the key."""
key = self.keyfunc(value)
+ if key is None:
+ from sqlalchemy.orm.session import object_session
+ object_session(value).flush() # ensure the primary key exists
+ key = self.keyfunc(value)
self.__setitem__(key, value, _sa_initiator)
set = collection.internally_instrumented(set)
set = collection.appender(set)
Comments (4)
-
-
repo owner it should be very trivial - literally take the four lines of code in the patch, and apply it instead within a custom "keyfunc", which can be sent as an argument to mapped_collection. Grok (this is for grok, right?) can just apply this function in the context of using their own rdb module. This works because Grok already assumes a particular session configuration that is compatible with the approach.
-
Account Deleted Sure, just thought I'd bring it up here as we don't seem to be the only ones running into it. (e.g. http://groups.google.com/group/sqlalchemy/browse_thread/thread/6eff084936fdb2f1/66ddfb3f6f0d13a0?lnk=gst&q=MappedCollection#)
-
repo owner - changed status to wontfix
there's nothing really to do on the SQLA end here
- Log in to comment
There are a number of problems with this patch:
It's pretty trivial to make your own custom mapped collection type that addresses this use case && apply it where the behavior is appropriate. I have doubts that the base classes could ever be patched to detect this use case automatically and do what you want here, but please feel free to prove me wrong. :)