Here is my example:
class Role(Base):
__tablename__ = 'role'
role_id = Column(BigInteger, primary_key=True)
role_name = Column(String, nullable=False)
role_description = Column(String)
class Group(Base):
__tablename__ = 'group'
group_id = Column(BigInteger, primary_key=True)
group_name = Column(String, nullable=False)
group_description = Column(String)
profiles = association_proxy('_profile_group_roles', 'profile')
class Profile(Base):
__tablename__ = 'profile'
profile_id = Column(BigInteger, primary_key=True)
profile_username = Column(String, nullable=False, unique=True)
groups = association_proxy('_profile_group_roles', 'group')
roles = association_proxy('_profile_group_roles', 'role')
class ProfileGroupRole(Base):
__tablename__ = 'profile_group_role_assoc'
profile_id = Column(BigInteger, ForeignKey('profile.profile_id'),
primary_key=True)
group_id = Column(BigInteger, ForeignKey('group.group_id'),
primary_key=True)
role_id = Column(BigInteger, ForeignKey('role.role_id'),
primary_key=True)
group = relationship(Group, backref=backref('_profile_group_roles',
cascade='all, delete-orphan'))
role = relationship(Role)
profile = relationship(Profile, backref=backref('_profile_group_roles',
cascade='all, delete-orphan'))
Example data:
s = db.connect(engine_config)
profile1 = db.Profile(profile_username='test_user')
group1 = db.Group(group_name='group1', group_description='')
role1 = db.Role(role_name='role1')
prg1 = db.ProfileGroupRole(profile=profile1, group=group1,
role=role1)
group2 = db.Group(group_name='group2', group_description='')
role2 = db.Role(role_name='role2')
prg2 = db.ProfileGroupRole(profile=profile1, group=group2,
role=role1)
prg3 = db.ProfileGroupRole(profile=profile1, group=group2,
role=role2)
s.add_all([prg1, prg2, prg3])
s.commit()
Querying:
db.Group.query.filter(db.Group.group_name ==
'group1').one().profiles[0].roles
produces:
[Role{'role_id': 4, 'role_name': 'role1', 'role_description': None},
Role{'role_id': 4, 'role_name': 'role1', 'role_description': None},
Role{'role_id': 5, 'role_name': 'role2', 'role_description': None}]
and now what I expected to get back was just one the one role associated
with 'group1':
[Role{'role_id': 4, 'role_name': 'role1', 'role_description': None}]
--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
http://www.sqlalchemy.org/
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable
Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.