Hey,
I'm trying to select on a property in my mapped object that I've overridden
with a property, as per
http://sqlalchemy.org/docs/adv_datamapping.myt#advdatamapping_properties_overriding
However, session.query().select_by() throws an InvalidRequestError, it can't
locate the property.
Attached is a minimal test that reproduces it.
My use case here is that I generate the hash at object creation time, and
update the hash if any of the attributes change, and need to select by that
hash because it becomes the secondary key for these objects.
Thanks!
(Also, is it possible for us bug reporting users to create accounts in the
SA trac? The last few I did as guest but of course never got notified about
your comments on the tickets)
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,
key='_hash'),
)
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,
)
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
-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Sqlalchemy-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users