On Sep 11, 2013, at 1:11 PM, Philip Scott <[email protected]> wrote:

> Hi Folks,
> 
> So we have a sort of generic table; let's call it 'Thing'. For the sake of 
> example, let it have two columns. An integer 'id', and a hstore 'data':
> 
> from sqlalchemy.dialects.postgresql import HSTORE
> from Column, Integer
> 
> class Thing(Base):
>     __tablename__ = 'thing'
> 
>     id          = Column(Integer, primary_key=True)
>     data        = Column(MutableDict.as_mutable(HSTORE))
> 
> 
> Now what I want to do is have some more mapped attributes that peek inside 
> the HSTORE. I am assuming I know something about the HSTORE structure when I 
> add this to the class definition:
> 
> foo  = column_property(expression.cast(data["foo"], Integer))
> 
> And this works pretty well; when I get an instance of Thing, I can ask it for 
> it's 'foo' and I get an integer back.
> 
> However it doesn't work if I assign to foo. The session notices the object is 
> dirty but doesn't know how to write back my changes.

a column_property() against a SQL expression by definition is not writable.  
Your table doesn't have a CAST trigger inside of it for when an "int" is 
written to it that would convert it back to a string.

in this case since "data" is already loaded fully as a single column you might 
as well just use a hybrid.


Attachment: signature.asc
Description: Message signed with OpenPGP using GPGMail

Reply via email to