Sqlalchemy : association table for many-to-many relationship between groups and members. 如何删除关系?

标签 sqlalchemy pylons

我设置了这些表:http://pastie.org/627764

...    
    # This is the association table for the many-to-many relationship between
    # groups and members - this is, the memberships.
    user_group_table = Table('user_group', metadata,
        Column('user_name', Integer, ForeignKey('user.user_name',
            onupdate="CASCADE", ondelete="CASCADE")),
        Column('group_name', Integer, ForeignKey('group.group_name',
            onupdate="CASCADE", ondelete="CASCADE"))
    )

    class Group(DeclarativeBase):

        __tablename__ = 'group'

        group_name = Column(Unicode(16), primary_key=True)

        users = relation('User', secondary=user_group_table, backref='groups')
...

我正在尝试删除一个用户与其组中的一个用户之间的关系,但我无法真正提出执行此操作的查询。有什么方法可以使用 Sqlalchemy 来做到这一点吗?

感谢您的宝贵时间。

最佳答案

您的意思是要从组中删除用户?

# fetch the mapped classes
group = Session.query(Group).some_filters().one()
user = Session.query(User).some_filters().one()

# group.users is a list of all users in this group
# remove one and it will be removed from the DB
group.users.remove( user )
Session.commit()

关于Sqlalchemy : association table for many-to-many relationship between groups and members. 如何删除关系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1467823/

相关文章:

python - 使用 Pyramid 服务文件

python - 禁用 sqlalchemy 事件监听器的方法

python - Flask-SQLAlchemy 查询计数

python - SQLAlchemy - 创建表模板

python - 使用 Flask Python 将点几何插入 PostGIS

python - Pyramid : "Unknown column ' 中的 OperationalError 1054、 'field list' XX'

python - Pylons + AuthKit 中的用户身份验证

python - 如何从 SqlAlchemy 创建和恢复备份?

python - Pylons 和 Memcached

java - Pylons 或 TurboGears 与 .NET 或 Java