On Dec 11, 2011, at 3:54 PM, Hector Blanco wrote:

> Hello everyone!
> 
> I am continuing with the migration from SqlAlchemy 0.6.8 to 0.7.4, and
> I've seen a little difference in behavior.
> 
> I have a class Product (more or less) like:
> 
> class Product(declarativeBase):
>        _id = Column("id", Integer, primary_key=True)
>       _model = Column("model", Unicode(128))
> 
>       @hybrid_property
>       def model(self):
>               return self._model
> 
>       @model.setter
>       def setModel(self, model):
>               if model:
>                       self._model = unicode(model)
>               else:
>                       self._model = None
> 
>         #id hybrid property as well

If the use case is really like what you have above, I'd probably just use the 
@validates decorator to coerce to unicode, and really I'd just use the 
TypeDecorator here:  
http://www.sqlalchemy.org/docs/core/types.html#coercing-encoded-strings-to-unicode

just something to consider...

> 
> Both id and model are hybrid_properties now (they were Synonyms in 0.6.8)
> 
> When I query only some values:
> 
> query = session.query(Product.Product).filter(Product.Product.id ==
> 25).values(Product.Product.model)
> for element in query:
>       print "%s\n" % vars(element)
> 
> The query works perfectly, but I get this:
> {'_model': u'myAifon', '_labels': ('_model',)}
> 
> The "hidden" (or masked... I don't know how to call it) attribute (the
> "private" one, starting with a "_") so if I try something like

the synonym approach would still have those in there, you'd just have your 
"other" attributes in there as well.... not sure what "Product.Product" is 
though, a class ?

> 
> for element in query:
>       print "The 'model' is: %s\n" % element.model
> 
> I get a "Kaboom!!" (I''ve heard it's the Swahili for AttributeError
> exception). Is there a workaround this?

see again, I don't understand why that is, this depends on how you wrote the 
__str__() function being called here.  If you're looking at the mapper's list 
of properties to do __str__(), that would be why.

The thing about hybrids is that they aren't mapped attributes.   This was 
supposed to be "simpler", in that they have no hidden behaviors.   But if 
you're depending on lots of attributes of mapped attributes then moving from 
synonym is a little more effort since you'd need to move away from that too.    
It's hard for me to document, "this is why synonyms are useful, you might want 
to do something based on mapper.properties", I'm not really super comfortable 
encouraging people to use that pattern.






-- 
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 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to