sqlalchemy.exc.CircularDependencyError: Dependência circular detectada

A lógica de negócios - Uma Categoria pode ter vários atributos (1: M), como Categoria "Memória" pode ter atributos Velocidade, Tamanho, Tipo etc.

ao mesmo tempo, uma Categoria pode ser classificada pelo valor do atributo (isso é armazenado dentro de Category.sortByAttribute - que é a chave estrangeira para a tabela LookupCategoryAttributes).

Tentando construí-lo via SQLAlchemy, mas detectando a dependência circular. O que está errado?

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")

e então o código se parece com isso:

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

category.Attributes.append(attr1)
category.Attributes.append(attr2)
category.SortByAttribute = attr1

quando eu executo commit eu recebo:

sqlalchemy.exc.CircularDependencyError: Circular dependency detected.

questionAnswers(1)

yourAnswerToTheQuestion