Michael -- I see your point on the ORM creating objects that are 
invariant on the primary keys and would use the combination of uid and 
timestamp as the pk.

What I can't seem to figure out is how to add additional constraints to 
the Mapper.

How would you define a mapper to always return the object defined by 
the combination of uid and max(timestamp)?

When I define a second mapper, I get the following:

from sqlalchemy import *
metadata = BoundMetaData('sqlite://', echo=True)

users = Table('users', metadata,
     Column('user_id', Integer, primary_key = True),
     Column('timestamp', Integer, primary_key = True),
     Column('name', String(80))
)

metadata.create_all()

ts = 100.
users.insert().execute(
     dict(user_id = 1, name = 'Tom', timestamp = ts),
     dict(user_id = 2, name = 'Dick', timestamp = ts),
     dict(user_id = 3, name = 'Harry', timestamp = ts),
     dict(user_id = 1, name = 'Tommy', timestamp = ts-1),
     dict(user_id = 1, name = 'Tomi', timestamp = ts-2),
     dict(user_id = 1, name = 'Tom', timestamp = ts-3),
)
class User(object) :
     pass

user_map = mapper(User, users)
s = select([users.c.user_id,
     func.max(users.c.timestamp).label('timestamp'), users.c.name])
user1_map = mapper(User, s.alias('t'), non_primary = True)

session = create_session()
q = session.query(user1_map)
q.select(users.c.user_id == 1)
[2006-08-07 11:13:25,845] [engine]: SELECT t.timestamp AS t_timestamp, 
t.user_id AS t_user_id, t.name AS t_name
FROM users, (SELECT users.user_id AS user_id, max(users.timestamp) AS 
timestamp, users.name AS name) AS t
WHERE users.user_id = ? ORDER BY t.oid
[2006-08-07 11:13:25,878] [engine]: [1]
[2006-08-07 11:13:25,880] [engine]: ROLLBACK
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
   File "build/bdist.macosx-10.3-fat/egg/sqlalchemy/orm/query.py", line 
226, in select
   File "build/bdist.macosx-10.3-fat/egg/sqlalchemy/orm/query.py", line 
232, in select_whereclause
   File "build/bdist.macosx-10.3-fat/egg/sqlalchemy/orm/query.py", line 
302, in _select_statement
   File "build/bdist.macosx-10.3-fat/egg/sqlalchemy/orm/query.py", line 
264, in instances
   File "build/bdist.macosx-10.3-fat/egg/sqlalchemy/orm/session.py", 
line 114, in execute
   File "/Users/noon/Developer/test/base.py", line 244, in execute

   File "/Users/noon/Developer/test/base.py", line 268, in 
execute_clauseelement

   File "/Users/noon/Developer/test/base.py", line 284, in 
execute_compiled

   File "/Users/noon/Developer/test/base.py", line 280, in proxy

   File "/Users/noon/Developer/test/base.py", line 319, in _execute_raw

   File "/Users/noon/Developer/test/base.py", line 338, in _execute

sqlalchemy.exceptions.SQLError: (OperationalError) no such column: 
users.user_id 'SELECT t.timestamp AS t_timestamp, t.user_id AS 
t_user_id, t.name AS t_name \nFROM users, (SELECT users.user_id AS 
user_id, max(users.timestamp) AS timestamp, users.name AS name) AS t 
\nWHERE users.user_id = ? ORDER BY t.oid' [1]


--Bill


-------------------------------------------------------------------------
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

Reply via email to