跨越SQLAlchemy中的四个表的关系

时间:2015-03-27 13:45:29

标签: python mysql sqlalchemy

我正在尝试建立一个跨越四个表的关系。我根据this question中的代码简化了我的代码以匹配我的数据库。

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

Base = declarative_base()


class A(Base):
    __tablename__ = 'a'

    id = Column(Integer, primary_key=True)
    b_id = Column(Integer, ForeignKey('b.id'))


    # FIXME: This fails with:
    #   "Relationship A.ds could not determine any unambiguous local/remote column pairs based on
    #    join condition and remote_side arguments.  Consider using the remote() annotation to
    #    accurately mark those elements of the join condition that are on the remote side of the relationship."
    #
    # ds = relationship("D", primaryjoin="and_(A.b_id == B.id, B.id == C.b_id, D.id == C.d_id)", viewonly=True)

    def dq(self):
        return sess.query(D).filter(and_(D.id == C.d_id,
                                         C.b_id == B.id,
                                         B.id == A.id,
                                         A.id == self.id))


class B(Base):
    __tablename__ = 'b'

    id = Column(Integer, primary_key=True)


class C(Base):
    __tablename__ = 'c'

    b_id = Column(Integer, ForeignKey('b.id'), primary_key=True)
    d_id = Column(Integer, ForeignKey('d.id'), primary_key=True)


class D(Base):
    __tablename__ = 'd'

    id = Column(Integer, primary_key=True)


e = create_engine("sqlite://", echo=True)
Base.metadata.create_all(e)

sess = Session(e)

sess.add(D(id=1))
sess.add(D(id=2))
sess.add(B(id=1))
sess.add(C(b_id=1, d_id=1))
sess.add(C(b_id=1, d_id=2))
sess.add(A(id=1, b_id=1))
sess.flush()


a1 = sess.query(A).first()
print a1.dq().all()
#print a1.ds

所以我的问题是' ds'的连接语法。关系。当前错误提到添加remote(),但我没有让它工作。我也尝试过使用secondaryjoin而没有运气。查询' dq'工作和我最终能够通过在我的代码中使用过滤器来解决它 - 我仍然很好奇如何在可能的情况下构建反关系?

2 个答案:

答案 0 :(得分:2)

我不是sqlalchemy专家,这是我的理解。

我认为sqlalchemy关系API中混淆的主要原因是参数primaryjoinsecondarysecondaryjoin的真正意义。对我来说,他们是:

        primaryjoin              secondaryjoin(optional)
source -------------> secondary -------------------------> dest
 (A)                                                        (D)

现在我们需要弄清楚中间部分应该是什么。尽管sqlalchemy中的自定义连接意外地复杂,但您确实需要了解您所要求的内容,即原始SQL。一种可能的解决方案是:

SELECT a.*, d.id 
FROM a JOIN (b JOIN c ON c.b_id = b.id JOIN d ON d.id = c.d_id) /* secondary */
ON a.b_id = b.id /* primaryjoin */ 
WHERE a.id = 1;

在这种情况下,来源a与“辅助”(b JOIN c .. JOIN d ..)加入,并且D没有辅助加入,因为它已经在secondary中。我们有

ds1 = relationship(
    'D',
    primaryjoin='A.b_id == B.id',
    secondary='join(B, C, B.id == C.b_id).join(D, C.d_id == D.id)',
    viewonly=True,  # almost always a better to add this
)

另一种解决方案可能是:

SELECT a.*, d.id 
FROM a JOIN (b JOIN c ON c.b_id = b.id) /* secondary */
ON a.b_id = b.id /* primaryjoin */
JOIN d ON c.d_id = d.id /* secondaryjoin */
WHERE a.id = 1;

此处a加入辅助(b JOIN c..),辅助加入dc.d_id = d.id,因此:

ds2 = relationship(
    'D',
    primaryjoin='A.b_id == B.id',
    secondary='join(B, C, B.id == C.b_id)',
    secondaryjoin='C.d_id == D.id',
    viewonly=True,  # almost always a better to add this
)

经验法则是您在辅助路径中放置长连接路径,并将其链接到源和目标。

在性能方面,ds1ds2导致查询计划比dq略微简单,但我认为它们之间没有太大区别。规划师总是知道的更好。

以下是供您参考的更新代码。请注意您如何热切地加载与sess.query(A).options(joinedload('ds1'))

的关系
from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()


class A(Base):
    __tablename__ = 'a'

    id = Column(Integer, primary_key=True)
    b_id = Column(Integer, ForeignKey('b.id'))

    ds1 = relationship(
        'D',
        primaryjoin='A.b_id == B.id',
        secondary='join(B, C, B.id == C.b_id).join(D, C.d_id == D.id)',
        viewonly=True,  # almost always a better to add this
    )
    ds2 = relationship(
        'D',
        secondary='join(B, C, B.id == C.b_id)',
        primaryjoin='A.b_id == B.id',
        secondaryjoin='C.d_id == D.id',
        viewonly=True,  # almost always a better to add this
    )

    def dq(self):
        return sess.query(D).filter(and_(D.id == C.d_id,
                                         C.b_id == B.id,
                                         B.id == A.id,
                                         A.id == self.id))


class B(Base):
    __tablename__ = 'b'

    id = Column(Integer, primary_key=True)


class C(Base):
    __tablename__ = 'c'

    b_id = Column(Integer, ForeignKey('b.id'), primary_key=True)
    d_id = Column(Integer, ForeignKey('d.id'), primary_key=True)


class D(Base):
    __tablename__ = 'd'

    id = Column(Integer, primary_key=True)

    def __repr__(self):
        return str(self.id)


e = create_engine("sqlite://", echo=True)
Base.metadata.drop_all(e)
Base.metadata.create_all(e)

sess = Session(e)

sess.add(D(id=1))
sess.add(D(id=2))
sess.add(B(id=1))
sess.add(B(id=2))
sess.flush()
sess.add(C(b_id=1, d_id=1))
sess.add(C(b_id=1, d_id=2))
sess.add(A(id=1, b_id=1))
sess.add(A(id=2, b_id=2))
sess.commit()


def get_ids(ds):
    return {d.id for d in ds}


a1 = sess.query(A).options(joinedload('ds1')).filter_by(id=1).first()
print('{} a1.ds1: {}'.format('=' * 30, a1.ds1))
assert get_ids(a1.dq()) == get_ids(a1.ds1)


a1 = sess.query(A).options(joinedload('ds2')).filter_by(id=1).first()
print('{} a1.ds2: {}'.format('=' * 30, a1.ds2))
assert get_ids(a1.dq()) == get_ids(a1.ds2)

a2 = sess.query(A).options(joinedload('ds2')).filter_by(id=2).first()
print('{} a2.ds1: {}; a2.ds2 {};'.format('=' * 30, a2.ds1, a2.ds2))
assert a2.ds1 == a2.ds2 == []

答案 1 :(得分:0)

对于您的查询,您需要将表格加在一起。即:

def db(self);
    return sess.query(D).join(B).filter(...)

我不确定你是否可以进行多次加入,但我不明白为什么不这样做。可以在此处找到对对象关系查询的良好参考:

http://docs.sqlalchemy.org/en/rel_0_9/orm/tutorial.html#working-with-related-objects