Ok.. given: CREATE TABLE example_a ( id integer NOT NULL, time_units integer, CONSTRAINT example_a_pkey PRIMARY KEY (id) )
and (based on http://www.sqlalchemy.org/docs/05/mappers.html#using-descriptors): from sqlalchemy import * from sqlalchemy.orm import sessionmaker, mapper, synonym class A(object): def _set_t(self, t): print "SET" self.timeunits = t / float(10) def _get_t(self): print "GET" return self.timeunits * 10 time_units = property(_get_t, _set_t) engine = create_engine("postgres://tobe:<snip>@localhost/test") engine.echo = False session_class = sessionmaker( bind = engine, autoflush = True, autocommit = False, ) meta = MetaData(bind = engine) table_A = Table("example_a", meta, autoload = True) print A.time_units mapper(A, table_A, properties = { "time_units" : synonym("timeunits", map_column = True) }) session = session_class() a = session.query(A).all()[0] print a.timeunits a.timeunits = 1 print a.timeunits print A.timeunits print A.time_units ~ ~ Running the above: t...@bismuth:~/src/tobe/bdp_webif$ python test.py <property object at 0x849a7fc> 10 1 <Mapper at 0x84f030c; A>.timeunits Traceback (most recent call last): File "test.py", line 36, in <module> print A.time_units File "/usr/lib/python2.5/site-packages/SQLAlchemy-0.5.0rc2-py2.5.egg/sqlalchemy/orm/attributes.py", line 121, in __str__ return repr(self.parententity) + "." + self.property.key File "/usr/lib/python2.5/site-packages/SQLAlchemy-0.5.0rc2-py2.5.egg/sqlalchemy/orm/attributes.py", line 184, in __getattr__ return getattr(descriptor, attribute) AttributeError: 'property' object has no attribute 'parententity' So you can clearly see that the get and set are not called when accessing timeunits. They *are* called when accessing time_units (not shown). If you substitute '_email' for 'timeunits' and 'email' for 'time_units' then I believe this is an identical pattern to that shown in the documentation on this:http://www.sqlalchemy.org/docs/05/mappers.html#using-descriptors. The same behaviour is observed if I try an example with manually specified columns. So.. again.. am I simply not getting something here, is the example misleading or wrong or is the code broke in some way. Thanks in advance, -- Toby Bradshaw Ideaworks 3d Ltd, London, UK. Michael Bayer wrote: > On Jan 23, 2009, at 1:43 PM, Toby Bradshaw wrote: > > the synonym() construct and the mapped attribute it creates represents > a proxy to the mapped column only in the context of accessing and > setting values within the python application space. It is not invoked > when the database populates the value of the mapped column itself. > the general idea of synonym is that the column-based information stays > represented on the mapped instance in the identical manner that it > does within the database, and the synonym-based attribute interprets > application-level values into the column representation. > > it should definitely be invoked when setting the property of an > existing instance of B, so if that's not working you can share with us > exactly how you are configuring things, since you might be missing > some detail. > > > > --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
