2013-08-17 33 views
9

iş mantığı tespit süresi bir Kategori bu Category.sortByAttribute içinde saklanır (öznitelik değerine göre sıralanmış olabilir -.. LookupCategoryAttributes masaya yabancı anahtar hangisqlalchemy.exc.CircularDependencyError Bir Kategori birden sahip olabilir Döngüsel bağımlılık

sqlalchemy yoluyla inşa çalışıyorum, ancak dairesel bağımlılık algılandı alma yanlış olan

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

ve sonra kod şuna benzer: Ben olsun taahhüt yürütmek zaman

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. 

cevap

14

Tamam cevap buldum - ilişkide kullanılması post_update http://docs.sqlalchemy.org/en/latest/orm/relationship_persistence.html#post-update

yani ne yaptım ise içinde Kategori sınıfı aşağıdaki gibi değiştirilmiştir:

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

:

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

itibaren SQLAlchemy 1.0 şunları yapabilirsiniz belirli koşullar için potansiyel olarak use_alter = Doğru: http://docs.sqlalchemy.org/en/latest/core/constraints.html#use-alter – Damian

+1

Maalesef, başka bir yoldan, use_alter <1.0 ve 1.0 sürümlerinde kullanılabilir ve yukarıdakini otomatik olarak algılar ve kullanır. – Damian

+0

Bu yanıttaki URL taşındı. Şimdi http://docs.sqlalchemy.org/en/latest/orm/relationship_persistence.html#post-update adresinde –

İlgili konular