I am trying to map to a simple read only property. According to the
docs, I *think* I am supposed to use synonym. The problem is that I am
getting a None value for the mapped descriptor's column.

For example:

import datetime
from sqlalchemy import Column, Table, Integer, String, MetaData,
create_engine
from sqlalchemy.orm import mapper, sessionmaker, synonym

meta = MetaData()
foo_table = Table('foo', meta,
    Column('id', String(3), primary_key=True),
    Column('description', String(64), nullable=False),
    Column('calculated_value', Integer, nullable=False),
    )

class Foo(object):
    def __init__(self, id, description):
        self.id = id
        self.description = description

    @property
    def calculated_value(self):
        self._calculated_value = datetime.datetime.now().second + 10
        return self._calculated_value

mapper(Foo, foo_table,
    properties = {
        'calculated_value' : synonym('_calculated_value',
map_column=True)
    }
)

engine = create_engine('sqlite:///test.db')
Session = sessionmaker(bind=engine)
meta.create_all(bind=engine)

session = Session()
session.add( Foo('xyz', 'test only') )
session.commit()


Since self._calculated_value is created within the property, it does
not exist until you actually access the property. How do I set this up
to get sqlalchemy to go use the actual return value of the property
when session.commit is called?

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