5 messages in com.googlegroups.sqlalchemy[sqlalchemy] Re: Questions on MapperE...
FromSent OnAttachments
Ross30 Jan 2008 10:36 
Michael Bayer30 Jan 2008 11:27 
jason kirtland30 Jan 2008 11:40 
Michael Bayer30 Jan 2008 11:48 
Ross30 Jan 2008 12:43 
Subject:[sqlalchemy] Re: Questions on MapperExtension
From:Michael Bayer (mike@zzzcomputing.com)
Date:01/30/2008 11:48:06 AM
List:com.googlegroups.sqlalchemy

oh, 0.3. well theres another question. If you just converted from SQLObject, why to SA 0.3 ? 0.4 is the currently supported version and is also vastly superior to 0.3.

On Jan 30, 2008, at 2:40 PM, jason kirtland wrote:

On 0.3.x, use EXT_PASS rather than EXT_CONTINUE.

sorry, the docstring is wrong. create_instance() should return EXT_CONTINUE if it would like to bypass creating the instance itself. However, "self" here is the MapperExtension instance, not the mapped instance. the method is called before anything is created.

if you want to populate an attribute on a newly loaded instance but not create it, you're better off using populate_instance(). return EXT_CONTINUE from that method as well.

class MyExt(MapperExtension): def populate_instance(self, mapper, selectcontext, row, instance, **flags): if not hasattr(instance, 'mutex'): instance.mutex = mutex() return EXT CONTINUE

the hasattr() is assuming you dont want to replace the mutex in the case of a session.refresh() or similar.

On Jan 30, 2008, at 1:36 PM, Ross wrote:

Hello,

I have recently converted a Pylons app from SQLObject to SQLAlchemy 0.3.10. Conversion went quite well.

I need to serialize access to some of my objects, so I've looked into extending MapperExtension as described at [1] to add a mutex on load.

First, I define an extension and instantiate it:

-------- from sqlalchemy.orm import MapperExtension import mutex

class MutexExtension(MapperExtension): def create_instance(self, mapper, selectcontext, row, class_): self.mutex = mutex.mutex() return None

mutexext = MutexExtension()

-------

My mapper setup looks like this:

switch_mapper = mapper (Switch, switch_table, extension=[mutexext.mutexext, sac.ext],

properties={'ports':sqla.relation(SwitchPort)})

When I try to fetch objects from the database, I get a exception setting self.entity_name:

Module sqlalchemy.orm.mapper:1485 in _instance << instance = self._create_instance(context.session) else: instance._entity_name = self.entity_name if self.__should_log_debug: self.__log_debug("_instance(): created new instance %s identity %s" % (mapperutil.instance_str(instance), %str(identitykey)))>> instance._entity_name = self.entity_name exceptions.AttributeError: 'NoneType' object has no attribute '_entity_name'

What did I do wrong?

[1]
http://www.sqlalchemy.org/docs/03/adv_datamapping.html#advdatamapping_extending