The tables are: user_t = Table('users', meta, Column('id', Integer, primary_key=True), Column('name', String(20), nullable=False), )
post_t = Table('posts', meta, Column('id', Integer, primary_key=True), Column('author_id', Integer, ForeignKey('users.id')), Column('title', String(200), nullable=False), Column('body', TEXT, nullable=False), ) And the mappers are: user_pcount_view = select( [c for c in user_t.c]+[func.count(post_t.c.id).label('post_count')], from_obj = [outerjoin(user_t, post_t)], group_by = [c for c in user_t.c], ).alias('user_pcount') mapper(User, user_pcount_view) mapper(Post, post_t, properties={ 'author': relation(User, lazy=False, backref='posts') }) But I found that User.post_count was always None . The full code is below: from sqlalchemy import * meta = DynamicMetaData() user_t = Table('users', meta, Column('id', Integer, primary_key=True), Column('name', String(20), nullable=False), ) post_t = Table('posts', meta, Column('id', Integer, primary_key=True), Column('author_id', Integer, ForeignKey('users.id')), Column('title', String(200), nullable=False), Column('body', TEXT, nullable=False), ) engine = create_engine('sqlite://') # in memory meta.connect(engine) meta.create_all() class User(object): def __init__(self, name): self.name = name class Post(object): def __init__(self, title, body): self.title = title self.body = body user_pcount_view = select( [c for c in user_t.c]+[func.count(post_t.c.id).label('post_count')], from_obj = [outerjoin(user_t, post_t)], group_by = [c for c in user_t.c], ).alias('user_pcount') mapper(User, user_pcount_view) mapper(Post, post_t, properties={ 'author': relation(User, lazy=False, backref='posts') }) session = create_session(bind_to=engine) engine.echo = False session.echo_uow = False user1 = User('user1') user1.posts.append(Post('post1', 'post1 content...')) user1.posts.append(Post('post2', 'post2 content...')) session.save(user1) session.flush() user = session.query(User).selectone_by(name='user1') assert user.post_count==2, 'user.post_count is %s'%user.post_count # AssertionError: user.post_count is None -- http://codeplayer.blogspot.com/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalchemy@googlegroups.com 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 -~----------~----~----~----~------~----~------~--~---