Thank you, Michael! On 22 Грд, 18:38, Michael Bayer <[email protected]> wrote: > Thank you, please apply the change in r9327b3748997 to your > _params_from_query() function. > > On Dec 22, 2010, at 5:47 AM, sector119 wrote: > > > > > > > > > from beaker import cache > > > from sqlalchemy import * > > from sqlalchemy.orm import mapper, sessionmaker, scoped_session, > > relationship > > from sqlalchemy.types import * > > > # from examples/beaker_caching > > from eps.model import caching_query > > > ### INIT > > > cache_manager = cache.CacheManager() > > metadata = MetaData() > > engine = create_engine('postgresql+psycopg2:// > > LOGIN:[email protected]:5432/DBNAME', echo=False) > > Session = scoped_session(sessionmaker(autoflush=True, > > autocommit=False, > > > query_cls=caching_query.query_callable(cache_manager), bind=engine)) > > > cache_manager.regions['default'] = { > > 'type': 'memory', > > 'lock_dir': '/tmp', > > } > > > ### END INIT > > > ### TABLES > > > # groups > > > groups_table = Table('groups', metadata, > > Column('id', Integer, primary_key=True), > > Column('name', String(255), unique=True, nullable=False) > > ) > > > class Group(object): > > def __init__(self, name): > > self.name = name > > > mapper(Group, groups_table) > > > # users > > > users_table = Table('users', metadata, > > Column('id', Integer, primary_key=True), > > Column('username', String(255), unique=True, nullable=False), > > Column('first_name', Unicode(255), nullable=False), > > Column('last_name', Unicode(255), nullable=False), > > Column('middle_name', Unicode(255), nullable=False) > > ) > > > # users_groups > > > users_groups_table = Table('users_groups', metadata, > > Column('user_id', Integer, ForeignKey('users.id')), > > Column('group_id', Integer, ForeignKey('groups.id')) > > ) > > > class User(object): > > def __init__(self, username, first_name, last_name, middle_name): > > self.username = username > > self.first_name = first_name > > self.last_name = last_name > > self.middle_name = middle_name > > > mapper( > > User, > > users_table, > > properties={ > > 'groups': relationship(Group, lazy=True, > > secondary=users_groups_table, backref='users') > > } > > ) > > > cache_user_relationships = caching_query.RelationshipCache('default', > > 'by_id', User.groups) > > > ### END TABLES > > > ### HELPERS > > > def get_user(username): > > return Session.query(User).\ > > options(cache_user_relationships).\ > > options(caching_query.FromCache('default', > > 'by_username')).\ > > filter_by(username=username).one() > > > def print_groups(user): > > for g in user.groups: > > print g.name > > > ### END HELPERS > > > ### CREATE > > > metadata.create_all(engine) > > > ### END CREATE > > > ### POPULATE > > > u1 = User('sector119', u'A', u'B', u'C') > > u1.groups = [Group('G1')] > > u2 = User('sector120', u'D', u'E', u'F') > > u2.groups = [Group('G2')] > > Session.add_all([u1, u2]) > > > Session.commit() > > > ### END POPULATE > > > ### TEST ... > > > u = get_user('sector119') > > print '1. %s groups:' % u.username > > print_groups(u) > > print '2. %s groups:' % u.username > > print_groups(u) > > > u = get_user('sector120') > > print '1. %s groups:' % u.username > > print_groups(u) > > print '2. %s groups:' % u.username > > print_groups(u) > > > -- > > 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 > > athttp://groups.google.com/group/sqlalchemy?hl=en.
-- 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.
