Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

sqlalchemy: how to define association object with declarative style?

1 view
Skip to first unread message

Jerry Fleming

unread,
Sep 7, 2010, 5:39:18 AM9/7/10
to
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.

Chris Withers

unread,
Sep 20, 2010, 2:29:44 PM9/20/10
to Jerry Fleming, pytho...@python.org
Hi Jerry,

For SQLAlchemy questions, you're better off asking on
sqlal...@googlegroups.com.

On 07/09/2010 10:39, Jerry Fleming wrote:
> 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.

Yes, declarative doesn't currently work like this.
The functionality could probably be added, but Michael Bayer would be
the man to decide how to do that :-)

Chris

--
Simplistix - Content Management, Batch Processing & Python Consulting
- http://www.simplistix.co.uk

0 new messages