This one time, at band camp, Michael Bayer wrote:
>the doc you mention explains it should be done like this:
>
>mapper(PropertyClass, table,properties = {
> '_hash' : table.c.hash
> }
> )
Ok, attached is the same program with the key on the table removed, and the
property added, and I get the same error. (SQLalchemy 0.2.5).
from sqlalchemy import Table, Column, Integer, String, mapper, create_engine, default_metadata, create_session
table = Table('t1',
Column('id', Integer, primary_key=True),
Column('a', String),
Column('b', String),
Column('hash', String)
)
class PropertyClass(object):
def __init__(self, a=None, b=None):
self.a = a
self.b = b
# hash is not set directly, but based on a and b
self._update_hash()
def _update_hash(self):
self._hash = "%s %s" % (self.a, self.b)
def _set_hash(self, value):
raise RuntimeError, "can't set c directly (value=%s)" % value
def _get_hash(self):
return self._hash
hash = property(_get_hash, _set_hash)
def __repr__(self):
return '<PropertyClass a="%s" b="%s" hash="%s">' % (self.a, self.b, self.hash)
mapper(PropertyClass, table,
properties = {'_hash': table.c.hash},
)
engine = create_engine("sqlite:///:memory:")
default_metadata.connect(engine)
default_metadata.create_all()
session = create_session()
p = PropertyClass(a="a", b="b")
print p.hash
assert p.hash == "a b"
session.save(p)
session.flush()
results = session.query(PropertyClass).select()
print "full results:", results
results = session.query(PropertyClass).select_by(hash="a b")
print "constraint results", results
assert len(results) > 0
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Sqlalchemy-users mailing list
Sqlalchemy-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users