On Apr 9, 2007, at 11:25 AM, Brendan Arnold wrote:

> Is there a way I can ensure that an empty instance of my Address
> object is grafted onto the Person object even when corresponding data
> is not found in the people table?

you can either create a layer of abstraction between the actual  
mapped property and the publically accessible field by using a property:

class User(object):
        def _get_address(self):
                if self._address is None:
                        self._address = Address()
                return self._address
        address = property(_get_address)

mapper(User, users, properties={"_address":relation(Address)})


or you can use a MapperExtension and override create_instance():

class MyExt(MapperExtension):
        def create_instance(self, mapper, selectcontext, row, class_):
                instance = User()
                if not row['address_id']:
                        instance.address = Address()
                return instance

mapper(User, users, properties=..., extension=MyExt())

in both cases, flushing the User object will create a new address row  
in the DB since you have the blank address object there...to avoid  
that, you might have to add more layers of abstraction to the public  
"address" property.

of course theres another view of this, which is why does your  
application prefer a "blank" Address entry to None?  the latter is  
more semantically correct.

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to