2013-08-17 27 views
9

logiki biznesowej - jedna kategoria może mieć wiele (1: M) atrybutów, takich jak kategoria „pamięć” może mieć atrybuty Speed, rozmiar, typ itdsqlalchemy.exc.CircularDependencyError: Circular zależność wykryto

w taki sam czas jednej kategorii mogą być posortowane według wartości atrybutu (jest przechowywany wewnątrz Category.sortByAttribute -.., która jest klucz obcy do tabeli LookupCategoryAttributes

Próba skonstruowania go za pośrednictwem SQLAlchemy, ale coraz wykryto zależność cykliczna Co jest nie tak

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

a następnie kod wygląda następująco:

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

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

kiedy wykonać popełnić uzyskać:

sqlalchemy.exc.CircularDependencyError: Circular dependency detected. 

Odpowiedz

14

Ok znalazłem odpowiedź - stosowanie post_update w relacji http://docs.sqlalchemy.org/en/latest/orm/relationship_persistence.html#post-update

więc co zrobiłem jest wewnątrz Kategoria klasa została zmieniona następująco:

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

do tego:

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

Od SQLAlchemy 1,0 roku można potencjalnie use_alter = True pewnych okolicznościach: http://docs.sqlalchemy.org/en/latest/core/constraints.html#use-alter – Damian

+1

Przykro mi, w drugą stronę, use_alter może być użyty w wersjach <1.0, a 1.0 i wyżej faktycznie wykrywa i używa tego automatycznie. – Damian

+0

Adres URL w tej odpowiedzi został przeniesiony. Jest teraz na http://docs.sqlalchemy.org/en/latest/orm/relationship_persistence.html#post-update –