P
P
prokoptsev2015-03-10 15:55:36
ORM
prokoptsev, 2015-03-10 15:55:36

How to filter a subquery using SQLAlchemy?

Good afternoon. I confess at once with alchemy familiar not so long ago.
There is the following structure:

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'))
)

It is necessary using this structure to pull out the objects (object) of the category (category) with their parameters (value-property). But at the same time, the parameters that are needed are available for this category. Example:
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

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question