sqlalchemy 多态多对多 [英] sqlalchemy polymorphic many-to-many

查看:27
本文介绍了sqlalchemy 多态多对多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望通过以下方式获得属于父类的对象列表:

I'm looking to have a list of objects belong to a parent class, in the following manner:

class A(object):
    __tablename__ = 'a'
    id = Column(Integer, primary_key=True)
    collection = relationship(.....) # contains an ordered list of [B, C, B, B, C, C, C, B, C, C, ...];

class B(object):
    __tablename__ = 'b'
    id = Column(Integer, primary_key=True)

class C(object):
    __tablename__ = 'c'
    id = Column(Integer, primary_key=True)

SQLAlchemy 示例文件夹有一个简单的多对一,在我的示例中,类 B 和 C 是 A 的父"类(而不是相反),但我不能终生我想出了如何将其反转为一对多,然后添加双向关系使其成为多对多.

The SQLAlchemy examples folder has a simple many-to-one where, in my example, classes B and C are "parent" classes of A (rather than the other way around), but I can't for the life of me work out how to reverse this into a one-to-many, and then add a bidirectional relationship so that it becomes many-to-many.

谁能帮我解决这个问题?

Can anyone help me out with this?

推荐答案

有点痛苦,AbstractConcreteBase 显然需要更多的润色,但它是这样的:

it's a little painful and AbstractConcreteBase apparently needs a bit more polish, but it's like this:

from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base, AbstractConcreteBase

Base = declarative_base()

class A(Base):
    __tablename__ = 'a'
    id = Column(Integer, primary_key=True)

class BC(AbstractConcreteBase, Base):
    pass

class B(BC):
    __tablename__ = 'b'
    id = Column(Integer, primary_key=True)

    a_id = Column(Integer, ForeignKey('a.id'))
    __mapper_args__ = {
        "polymorphic_identity": "b",
        "concrete": True
    }

class C(BC):
    __tablename__ = 'c'
    id = Column(Integer, primary_key=True)
    a_id = Column(Integer, ForeignKey('a.id'))
    __mapper_args__ = {
        "polymorphic_identity": "c",
        "concrete": True
    }

configure_mappers()
A.collection = relationship(BC, primaryjoin=BC.a_id == A.id)

engine = create_engine("sqlite://", echo=True)

Base.metadata.create_all(engine)

sess = Session(engine)

sess.add_all([
    A(collection=[
        B(),
        C(),
        C()
    ]),
    A(collection=[
        B(),
        B()
    ])
])

sess.commit()

for a in sess.query(A):
    for bc in a.collection:
        print a, bc

这篇关于sqlalchemy 多态多对多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆