sqlalchemy.exc.CircularDependencyError:检测到循环依赖

编程入门 行业动态 更新时间:2024-10-28 20:17:40
本文介绍了sqlalchemy.exc.CircularDependencyError:检测到循环依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

业务逻辑-一个类别可能具有多个(1:M)属性,例如类别内存可能具有速度,大小,类型等属性。

The business logic - One Category may have multiple (1:M) attributes, like Category "Memory" could have attributes Speed, Size, Type etc.

at同时可以按属性值对一个Category进行排序(该属性存储在Category.sortByAttribute中-这是LookupCategoryAttributes表的外键。

at the same time one Category could be sorted by the attribute value (this is stored inside Category.sortByAttribute - which is foreign key to LookupCategoryAttributes table.

尝试通过SQLAlchemy构造它

Trying to construct it via SQLAlchemy, but getting circular dependency detected. What is wrong?

class Attribute(Base): __tablename__ = "LookupCategoryAttributes" types = ["date", "float", "integer", "select", "string", "text"] # Properties ID = Column(BigInteger, primary_key=True) categoryID = Column(BigInteger, ForeignKey('LookupCategories.ID'), nullable=False ) attribute = Column(VARCHAR(255), nullable=False) listValues = Column(VARCHAR(4000)) typeID = Column(VARCHAR(40), nullable=False) isRequired = Column(SmallInteger, nullable=False, default=0) displayInMenu = Column(SmallInteger, nullable=False, default=0) displayInFilter = Column(SmallInteger, nullable=False, default=0) class Category(Base): __tablename__ = "LookupCategories" # Properties ID = Column(BigInteger, primary_key=True) category = Column(VARCHAR(255), nullable=False) description = Column(VARCHAR(1000), nullable=False) parentCategoryID = Column(BigInteger, ForeignKey('LookupCategories.ID')) leftPos = Column(Integer) rightPos = Column(Integer) sortByAttribute = Column(BigInteger, ForeignKey('LookupCategoryAttributes.ID')) sortOrder = Column(SmallInteger, default=1) # Relationships ParentCategory = relationship("Category", uselist=False, remote_side=[ID], backref='SubCategories') SortByAttribute = relationship("Attribute", uselist=False, foreign_keys=[sortByAttribute], primaryjoin="Attribute.ID==Category.sortByAttribute") Attributes = relationship("Attribute", backref="Category", primaryjoin="Attribute.categoryID==Category.ID")

,然后代码如下所示:

category = Category(record['Name'], extID=extID) attr1 = Attribute(v) attr2 = Attribute(v) category.Attributes.append(attr1) category.Attributes.append(attr2) category.SortByAttribute = attr1

当我执行提交时,我得到:

when I execute commit I get:

sqlalchemy.exc.CircularDependencyError: Circular dependency detected.

推荐答案

确定了答案-在关系$ b中使用post_update $ b docs.sqlalchemy/en/latest /orm/relationship_persistence.html#post-update

Okay found the answer - use post_update in relationship docs.sqlalchemy/en/latest/orm/relationship_persistence.html#post-update

所以我在 Category 类中所做的更改如下:

so what I did is inside Category class is changed this:

SortByAttribute = relationship( "Attribute", uselist=False, foreign_keys=[sortByAttribute], primaryjoin="Attribute.ID==Category.sortByAttribute" )

为此:

SortByAttribute = relationship( "Attribute", uselist=False, foreign_keys=[sortByAttribute], primaryjoin="Attribute.ID==Category.sortByAttribute", post_update=True )

更多推荐

sqlalchemy.exc.CircularDependencyError:检测到循环依赖

本文发布于:2023-10-30 21:54:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1544112.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:检测到   sqlalchemy   exc   CircularDependencyError

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!