2

我正在尝试通过 SQLAlchemy 创建一个有向图(带有向后引用);如何获得下面列出的所需输出?[参见标题为“期望输出”的部分]


示例代码:

example_db.py

from sqlalchemy import Column, Integer, ForeignKey, Table
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, backref

Base = declarative_base()

node_connector = Table('node_connector', Base.metadata, 
    Column('from_node_handle', Integer, ForeignKey('node.handle')),
    Column('to_node_handle', Integer, ForeignKey('node.handle')))


class Node(Base):
    __tablename__ = 'node'
    handle = Column(Integer, nullable=False, primary_key=True)
    next_nodes = relationship('Node',
        secondary=node_connector,
        foreign_keys=[node_connector.c.to_node_handle],
        backref=backref("previous_nodes",
            foreign_keys=[node_connector.c.from_node_handle]))

测试/example_db_test.py

正在执行的模块

from sqlalchemy import create_engine, event
from sqlalchemy.engine import Engine
from sqlalchemy.orm import sessionmaker
import unittest

from ..example_db import Base, Node

class TestNodeDB(unittest.TestCase):

    def setUp(self):
        engine = create_engine('sqlite:///:memory:', echo=False)    
        SessionMaker = sessionmaker(bind=engine)
        self.__session = SessionMaker()
        Base.metadata.create_all(engine)

    def test_node_creation(self):
        node_a = Node()
        node_b = Node()
        node_c = Node()
        self.__session.add(node_a)
        self.__session.add(node_b)
        self.__session.add(node_c)
        node_a.next_nodes.append(node_b)
        node_b.next_nodes.append(node_c)
        self.__session.commit()

        nodes = self.__session.query(Node)

        for node in nodes:
            print "Node handle " + str(node.handle) + ":"
            for next_node in node.next_nodes:
                print "\t next node handle " + str(next_node.handle)
            for previous_node in node.previous_nodes:
                print "\t previous node handle " + str(previous_node.handle)



if __name__ == "__main__":
    unittest.main()

输出

实际输出

Node handle 1:
Node handle 2:
     next node handle 2
     previous node handle 2
Node handle 3:
     next node handle 3
     previous node handle 3

期望的输出

Node handle 1:
     next node handle 2
Node handle 2:
     next node handle 3
     previous node handle 1
Node handle 3:
     previous node handle 2

我怎样才能得到这个想要的输出?

谢谢!

4

1 回答 1

3

SQLAlchemy 甚至有这方面的文档:Self-Referential Many-to-Many Relationship,它准确地涵盖了你想要的。基本上,您几乎是正确的,只是没有使用foreign_keys设置 use primaryjoinand secondaryjoin

next_nodes = relationship('Node',
    secondary=node_connector,
    primaryjoin=handle==node_connector.c.to_node_handle,
    secondaryjoin=handle==node_connector.c.from_node_handle,
    backref="previous_nodes",
    )

现在你的测试工作了。一个额外的细节:您不需要添加node_band node_c:它们是通过 casacde 添加的:如果node_a在会话中并且您将某些内容添加到配置的关系中,它们也将被添加。您也不需要commit:当您查询会话数字时,它必须先刷新,然后再查询。

还要注意你需要注意这里的超载策略。以配置自引用渴望加载为例,找出适合您的方法。

于 2013-09-02T16:37:00.287 回答