简体   繁体   English

SQLAlchemy使用Association配置与self的多对多关系

[英]SQLAlchemy configuring many-to-many relationship to self using Association

I am having problems configuring a many to many relationship to a model itself. 我遇到了与模型本身配置多对多关系的问题。 I can configure a self-many-to-many relation when I use a normal relationship configuration ie one not using an Association object. 当我使用普通关系配置(即不使用Association对象)时,我可以配置自多对多关系。

In this scenario I have to record some extra information in the many-to-many table itself so I'm trying to implement relationship using an Association object (PageLink). 在这种情况下,我必须在多对多表中记录一些额外的信息,所以我试图使用Association对象(PageLink)来实现关系。

Here are the models. 这是模型。

class PageLink(Base):
    '''
    Association table.
    '''
    __tablename__ = 'page_links'

    id = Column(Integer,primary_key=True)
    page_from = Column(Integer,ForeignKey('page.id'),primary_key=True)
    page_to = Column(Integer,ForeignKey('page.id'),primary_key=True)
    extra_col1 = Column(String(256),nullable=False)

class Page(Base):
    '''
    main table
    '''

    __tablename__ = 'page'

    id = Column(Integer,primary_key=True)
    name = Column(String(56),nullable=False)

    linked = relationship('PageLinks',backref='parent_page',
                          primaryjoin=id==PageLink.page_from,
                          secondaryjoin=id==PageLink.page_to)

This approach does not work. 这种方法不起作用。 I have tried removing 'secondaryjoin' keyword but it wouldn't work. 我试过删除'secondaryjoin'关键字,但它不起作用。

Would greatly appreciate any help or advice on this matter. 非常感谢有关此事的任何帮助或建议。

Thank you for reading. 谢谢你的阅读。

The association object pattern is not a sepecialization of the many-to-many relationship, but rather a special case of one-to-many relationships where you have a left_table - Many-To-One - association_table - One-To-Many - right_table set up. 关联对象模式不是多对多关系的特殊化,而是一对多关系的特殊情况,其中你有一个left_table - Many-To-One - association_table - 一对多 - right_table建立。 In short, you need two relationships, neither of which should have a secondary / secondaryjoin . 简而言之,您需要两个关系,这两个关系都不应该具有secondary / secondaryjoin

class PageLink(Base):
    '''
    Association table.
    '''
    __tablename__ = 'page_links'

    id = Column(Integer,primary_key=True)
    page_from = Column(Integer,ForeignKey('page.id'),primary_key=True)
    page_to = Column(Integer,ForeignKey('page.id'),primary_key=True)
    extra_col1 = Column(String(256),nullable=False)

class Page(Base):
    '''
    main table
    '''

    __tablename__ = 'page'

    id = Column(Integer,primary_key=True)
    name = Column(String(56),nullable=False)

    linked_to = relationship('PageLinks',backref='parent_page',
                             primaryjoin=id==PageLink.page_from)
    linked_from = relationship('PageLinks',backref='child_page',
                               primaryjoin=id==PageLink.page_to)

which means, to access the extra column for the 'to' links from some page p , you have to do: p.linked_to[0].extra_col1 , or to get the actual linked page, p.linked_to[0].page_to 这意味着,要从某个页面p访问'to'链接的额外列,您必须执行: p.linked_to[0].extra_col1 ,或者获取实际的链接页面, p.linked_to[0].page_to


As an aside, it's often a great idea to use either an autoincrement primary key or (left/right) foreign key pair as the primary key in associations, but almost never useful to have both in the primary key. 顺便说一句,使用自动增量主键或(左/右)外键对作为关联中的主键通常是一个好主意,但在主键中同时使用它们几乎没有用。 An alternative that combines both ideas would be to use an autoincrement integer as the only column in the primary key, and have an additional unique constraint on the left/right foreign key columns. 结合这两种想法的替代方案是使用自动增量整数作为主键中的唯一列,并在左/右外键列上具有附加的唯一约束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 自引用多对多关系与 SQLAlchemy 中的关联 object - Self-referencing many-to-many relationship with an association object in SQLAlchemy 多对多关联表上的SQLAlchemy关系 - SQLAlchemy relationship on many-to-many association table 通过SqlAlchemy中的关联对象实现多对多,自引用,非对称关系(推特模型) - Many-to-many, self-referential, non-symmetrical relationship (twitter model) via Association Object in SqlAlchemy SQLAlchemy使用关联表以多对多关系插入数据 - SQLAlchemy Inserting Data in a Many-to-Many Relationship with Association Table 自引用多对多关系与关联 object 中的额外列 - Self referencing many-to-many relationship with extra column in association object SQLAlchemy - 与额外列的自引用多对多关系 - SQLAlchemy - Self referential Many-to-many relationship with extra column SQLAlchemy多对多关联类 - SQLAlchemy many-to-many association class SQLAlchemy:创建多对多并填充关联 - SQLAlchemy: create many-to-many and populate association 如何在SQLAlchemy中与关联对象(没有关联代理)建立多对多关系? - How can I access extra columns in SQLAlchemy a many-to-many relationship with an association object (without an association proxy)? SqlAlchemy关系与其他多对多关系 - SqlAlchemy relationship many-to-many to an other many-to-many
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM