SQLAlchemy中的复杂过滤器 - 如何?

时间:2013-01-09 05:45:47

标签: sqlalchemy

Attribute和AttributeValue是一对多,属性/ AttributeValue对,Item是多对多。 这个设计是允许一个Item可以附加几个Attributes。现在我们要选择所有具有所选Attribute-AttributeValue的项目。

我们想要选择具有特殊AttributeName / Value对的所有项目。 无论如何要在SQLAlchemy中执行此操作?

class AttributeSchema(Base, DBBase):
    __tablename__ = 'attributeschemas'

    id = Column(Integer, primary_key=True)
    name = Column(String(length=255))
    type = Column(Enum('Boolean', 'Integer', 'String'), default='String')
    is_index = Column(Boolean, default=False)

class AttributeValueSchema(Base, DBBase):
    __tablename__ = 'attributevalueschemas'
    id = Column(Integer, primary_key=True)
    value = Column(String(length=255))
    attributeschema_id = Column(Integer, ForeignKey('attributeschemas.id'), nullable=True)
    attributeschema = relationship('AttributeSchema', backref='attributeschemavalues')

class ItemType(Base, DBBase):
    __tablename__ = 'itemtypes'

    id = Column(Integer, primary_key=True)
    name = Column(String(length=255))

    market_price = Column(Float)
    attributes = relationship('AttributeSchema')

class ItemTypeAttribute(Base, DBBase):
    __tablename__ = 'itemtype_attribute'
    itemtype_id = Column(Integer, ForeignKey('itemtypes.id'), primary_key=True)
    attribute_id = Column(Integer, ForeignKey('attributeschemas.id'), primary_key=True)
    attributevalue_id = Column(Integer, ForeignKey('attributevalueschemas.id'), primary_key=True)
    itemtype = relationship('ItemType')
    attribute = relationship('AttributeSchema')
    attributevalue = relationship('AttributeValueSchema')

0 个答案:

没有答案