2013-08-17 29 views
9

Logic kinh doanh - Một loại có thể có nhiều (1: M) thuộc tính, giống như loại "Memory" có thể có các thuộc tính Tốc độ, kích cỡ, loại vvsqlalchemy.exc.CircularDependencyError: Thông tư phụ thuộc phát hiện

đồng Hiện một loại có thể được sắp xếp theo giá trị thuộc tính (điều này được lưu trữ bên trong Category.sortByAttribute -.. đó là khóa ngoại đến bảng LookupCategoryAttributes

đang cố gắng để xây dựng nó qua SQLAlchemy, nhưng nhận được phụ thuộc vòng tròn phát hiện gì là sai

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

và sau đó là mã trông như thế này:

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

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

khi tôi thực hiện cam kết tôi nhận được:

sqlalchemy.exc.CircularDependencyError: Circular dependency detected. 

Trả lời

14

rồi tìm thấy câu trả lời - post_update sử dụng trong mối quan hệ http://docs.sqlalchemy.org/en/latest/orm/relationship_persistence.html#post-update

vì vậy những gì tôi đã làm là bên trong Danh mục lớp học được thay đổi:

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

này:

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

Từ SQLAlchemy 1.0 trở đi bạn có thể có khả năng use_alter = True cho một số trường hợp: http://docs.sqlalchemy.org/en/latest/core/constraints.html#use-alter – Damian

+1

Xin lỗi, theo cách khác, use_alter có thể được sử dụng trong các phiên bản <1.0 và 1.0 trở lên thực sự phát hiện và sử dụng tự động. – Damian

+0

URL trong câu trả lời này đã chuyển. Hiện tại, tại http://docs.sqlalchemy.org/en/latest/orm/relationship_persistence.html#post-update –

Các vấn đề liên quan