Attribute 和 AttributeValue 是一对多的,Attribute/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')