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

德米特里·谢梅诺夫(DmitrySemenov)

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

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

试图通过SQLAlchemy构造它,但是检测到循环依赖。怎么了?

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

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

sqlalchemy.exc.CircularDependencyError: Circular dependency detected.
德米特里·谢梅诺夫(DmitrySemenov)

好的找到了答案-在关系http://docs.sqlalchemy.org/en/latest/orm/relationship_persistence.html#post-update中使用post_update

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

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
)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

SQLAlchemy-循环数据-CircularDependencyError

识别sqlalchemy.exc.OperationalError

烧瓶中的sqlalchemy.orm.exc.UnmappedInstanceError

如何更正此sqlalchemy.exc.NoForeignKeysError?

为什么我收到sqlalchemy.exc.ProgrammingError而不是sqlalchemy.exc.IntegrityError?

sqlalchemy.exc.ResourceClosedError:此连接已关闭错误

尝试运行查询的pymysql错误:sqlalchemy.exc.ProgrammingError

如何解决sqlalchemy.orm.exc.UnmappedInstanceError

Sqlalchemy.exc.OperationalError:(psycopg2.OperationalError)

如何修复烧瓶中的sqlalchemy.exc.NoForeignKeysError?

sqlalchemy.exc.IntegrityError:唯一约束失败

烧瓶-SQLAlchemy有很多原因导致sqlalchemy.orm.exc.FlushError

sqlalchemy.exc.ArgumentError:无法加载插件:sqlalchemy.dialects:driver

Flask-SQLAlchemy sqlalchemy.exc.OperationalError 没有这样的表“漫画”

sqlalchemy.exc.NoSuchModuleError:无法加载插件:sqlalchemy.dialects:bigquery

烧瓶错误:sqlalchemy.exc.NoSuchModuleError:无法加载插件:sqlalchemy.dialects:mongodb

无法使用 SqlAlchemy 创建数据库 (sqlalchemy.exc.OperationalError)

在Angular 7中检测到循环依赖

检测到Angular 4循环依赖

Angular 4-检测到循环依赖

循环依赖检测到的猫鼬错误

Angular 5循环依赖检测到警告

用VLOOKUP检测到循环依赖

在 powerbi 中检测到循环依赖

检测到重复值的烧瓶SQLAlchemy时停止插入表

Alembic-SQLAlchemy无法检测到现有表

SQLAlchemy未检测到_determine_joins上的ForeignKey,NoForeignKeysError

尝试使用SQLAlchemy更新数据库时,烧瓶sqlalchemy.exc.OperationalError:(OperationalError)没有此类表

sqlalchemy.exc.OperationalError:(psycopg2.OperationalError)fe_sendauth:未提供密码