I am trying to integrate with an existing user-group-role table structure 
where a user can belong to many groups and have multiple roles on each 
group.


I found a similar question to this, however it does not allow for multiple 
roles: Many-to-many declarative SQLAlchemy definition for users, groups, 
and roles 
<https://stackoverflow.com/questions/4864240/many-to-many-declarative-sqlalchemy-definition-for-users-groups-and-roles>


<https://stackoverflow.com/questions/4864240/many-to-many-declarative-sqlalchemy-definition-for-users-groups-and-roles>

I have the following table structure and would like to be able to access 
the roles in the following sort of manner: group.users[0].roles

It would also be nice to be able to access it from the other directions but 
is not required i.e. user.groups[0].roles or role.groups[0].users


class Role(Base):
    __tablename__ = 'roles'
    id = Column(Integer, primary_key=True, autoincrement=True)
    name = Column(Unicode(16), unique=True)
class User(Base):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True, autoincrement=True)
    name = Column(Unicode(16), unique=True)
class Group(Base):
    __tablename__ = 'groups'
    id = Column(Integer, primary_key=True, autoincrement=True)
    name = Column(Unicode(16), unique=True)
class UserGroupRole(Base):
    __tablename__ = 'user_group_role'
    id = Column(Integer, primary_key=True, autoincrement=True)
    user_id = Column(Integer, ForeignKey('users.id', ondelete='CASCADE'), 
nullable=False)
    group_id = Column(Integer, ForeignKey('groups.id', ondelete='CASCADE'), 
nullable=False)
    role_id = Column(Integer, ForeignKey('roles.id', ondelete='CASCADE'), 
nullable=False)


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

Reply via email to