sqlalchemy: how to define association object with declarative style?

Jerry Fleming jerry.fleming at saybot.com
Tue Sep 7 11:39:18 CEST 2010


Hi,

I want to define the relationship for my users and their groups with
declarative style (so that the relating model can inherit Timestamp
mixin and Tablename mixin):

class User(DeclarativeBase, Tablename, TimestampMixin):
    '''User avatar is named after its id.
    A user may be a student or teacher or both.
    '''
    id = Column(Integer, Sequence('user_id_seq'), primary_key=True)
    username = Column(String(30), index=True, nullable=False, unique=True)
    password = Column(String(30), index=True, nullable=False)

class Group(DeclarativeBase, Tablename, TimestampMixin):
    id = Column(Integer, Sequence('group_id_seq'), primary_key=True)
    name = Column(Unicode(20), unique=True, nullable=False)
    display = Column(Unicode(255))

class GroupUser(DeclarativeBase, Tablename, TimestampMixin):
    id = Column(Integer, Sequence('group_user_id_seq'), primary_key=True)
    user = Column(Integer, ForeignKey('user.id'), index=True),
    group = Column(Integer, ForeignKey('group.id'), index=True)

I was wondering how to associate User and Group with GroupUser with
GroupUser as the association object/proxy? The sqlalchemy docs only
mention only mention association tables, or non-declarative manual mapping.

Thanks.



More information about the Python-list mailing list