如何使用SQLAlchemy过滤器子查询?

时间:2015-03-10 13:45:34

标签: python orm flask sqlalchemy

我使用SQLAlchemy的时间不长。 我有那个结构:

class Category(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(140))
    parent_id = db.Column(db.Integer, db.ForeignKey('category.id'))
    parent = db.relationship("Category", remote_side=id, backref="sub_categories")

    def __repr__(self):
        return '<Category: {0.name}>'.format(self)

class Object(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(140))
    category_id = db.Column(db.Integer, db.ForeignKey('category.id'), nullable=False)
    category = db.relationship('Category', backref="objects")
    values = db.relationship("Value", secondary="object_value", backref="objects")

    def __repr__(self):
        return '<Object: {0.name}>'.format(self)


class Property(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(140))
    categories = db.relationship("Category", secondary="category_property", backref="properties")

    def __repr__(self):
        return '<Property: {0.name}>'.format(self)


class Value(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    value = db.Column(db.String(140))
    property_id = db.Column(db.Integer, db.ForeignKey("property.id"))
    prop = db.relationship('Property', backref="values")

    def __repr__(self):
        return '<Value: {0.prop.name}={0.value}>'.format(self)

db.Table(
    'category_property', db.Model.metadata,
    db.Column('category_id', db.Integer, db.ForeignKey('category.id')),
    db.Column('property_id', db.Integer, db.ForeignKey('property.id'))
)

db.Table(
    'object_value', db.Model.metadata,
    db.Column('object_id', db.Integer, db.ForeignKey('object.id')),
    db.Column('value_id', db.Integer, db.ForeignKey('value.id'))
)

使用此我尝试获取类别的所有对象,但在对象中我只想获得在类别中使用的属性。我的意思是这样的:

category = Category(name="Category")

category_property = Property(name="Category Property")
category.properties.append(category_property)
value_category_property = Value(prop=category_property, value="value")

other_property = Property(name="Other Property")
value_other_property = Value(prop=other_property, value="value")

obj = Object(name="Entry", category=category)
obj.values.append(value_category_property)
obj.values.append(value_other_property)

db.session.add(obj)
db.session.commit()

for obj in db.session.query(Object)\
                     .some_join_subquery()\
                     .filter(Object.category == category.id)\
                     .all():
    assert value_category_property in obj.values
    assert value_other_property not in obj.values

some_join_subquery必须包含哪些内容?

1 个答案:

答案 0 :(得分:0)

使用关系的any查询属性。

Object.query.filter(
    Object.category_id == category.id,
    Object.values.any(name == 'Category Property'),
    db.not_(Object.values.any(name == 'Other Property'))
)