On Jul 13, 2010, at 10:21 AM, sacabuche wrote:

> I was trying to get the last value "before_insert" a new item but i
> didn't find out, so i change of strategy and i saved the last value of
> this method, but the problem is that when i do other commits my old
> MapperExtension attribute (self.last_value) get the last value and i
> dislike it, even when I close and open a new Session (discovered in my
> tests)
> 
> This is my code:
> 
> class _ValueExtension(MapperExtension):
>    """Set new value for SkuDynamic and SKuUnique"""
>    def __init__(self):
>        self.last_value = {} # <<--- THE PROBLEM
>        self.first_value = FIRST_VALUE
> 
> 
>    def before_insert(self, mapper, connection, instance):
>        session = object_session(instance)
>       cls = instance.__class__
> 
>       last_sku_value = self.get_last_value(session, cls, instance)
> 
> 
>        if not last_sku_value:
>           instance.value = self.first_value
>       else:
>           instance.value = next_value(last_sku_value)
> 
>       self.last_value[self.last_value_key] = instance.value
>        self.set_values(instance)
> 
>    #def after_insert(self, mapper, connection, instance):
>    #    self.__init__()   #<--- IF I DO THIS I DON'T HAVE THE LAST
> VALUE IN THE SAME FLUSH
> 
>    #def after_update(self, mapper, connection, instance):
>    #    self.__init__()
> 
> 
>    def get_last_value(self, session, cls, instance):
>        """This have to return just one value
>       and set self.last_value_key is nedded to update elements in same
> session
>        Execute The Query or check self.last_value
>       """
>        raise NotImplementedError, 'implement this function'
> 
>    def set_values(self, instance):
>        """Use if necesary"""
>       pass


A MapperExtension is persistent for the life of the application and also is 
accessed concurrently, so it is not an appropriate place to store a count for 
things things that are local to a specific flush, and its really not a good 
place in general to do what you're trying to do (though its not clear above 
what you're trying to accomplish, if you want a global "last sku" or a "sku" 
specific to one flush).

You'd be better off querying the database for the "last sku value" when you 
need it, or just adding code to your classes __init__() method to keep track of 
sku numbers in memory as needed.




> 
> 
> Thanks
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To post to this group, send email to [email protected].
> 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.
> 

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to [email protected].
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