按关联表选择项目

时间:2014-10-09 15:48:46

标签: flask sqlalchemy

我正致力于构建食谱数据库。我试图建立一个查询,我得到包含某种成分的所有收件人(例如洋葱,胡萝卜),但我不是如何构建我的查询。我特意想要获得一个收件人列表(给定适当数量的连接)有一个Ingredient.name ='洋葱'。我的模型如下:

ingredients = db.Table('ingredients',
    db.Column('modified_ingredient', db.Integer, db.ForeignKey('modified_ingredient.id')),
    db.Column('ingredient', db.Integer, db.ForeignKey('ingredient.id'))
    )

modifiers = db.Table('modifiers',
    db.Column('modified_ingredient', db.Integer, db.ForeignKey('modified_ingredient.id')),
    db.Column('modifier', db.Integer, db.ForeignKey('modifier.id'))
    )

modified_ingredients = db.Table('modified_ingredients',
    db.Column('recipe', db.Integer, db.ForeignKey('recipe.id')),
    db.Column('modified_ingredient', db.Integer, db.ForeignKey('modified_ingredient.id'))
    )

class Recipe(db.Model):
    __tablename__ = 'recipe'

    id = db.Column(db.Integer, primary_key=True)

    name = db.Column(db.String(256))
    description = db.Column(db.Text)
    directions = db.Column(db.Text)
    prep_time = db.Column(db.Integer)
    cook_time = db.Column(db.Integer)
    image = db.Column(db.LargeBinary())
    ingredients = db.relationship('ModifiedIngredient', secondary=modified_ingredients)

class Ingredient(db.Model):
    __tablename__ = 'ingredient'

    id = db.Column(db.Integer, primary_key=True)

    name = db.Column(db.String(30), index=True, unique=True)

class Modifier(db.Model):
    __tablename__ = 'modifier'

    id = db.Column(db.Integer, primary_key=True)

    name = db.Column(db.String(30), index=True, unique=True)

class ModifiedIngredient(db.Model):
    __tablename__ = 'modified_ingredient'

    id = db.Column(db.Integer, primary_key=True)

    amount = db.Column(db.Integer)
    unit = db.Column(db.String(20))
    ingredients = db.relationship('Ingredient', secondary=ingredients, 
        backref=db.backref('ingredients', lazy='dynamic'), lazy='dynamic')
    modifiers = db.relationship('Modifier', secondary=modifiers,
        backref=db.backref('modifiers', lazy='dynamic'), lazy='dynamic')

主要是我对SQL和SQLAlchemy缺乏经验,这让我很难过。我知道我加入了一些东西,但我并不确定如何用一种有效的方式来表达它。

1 个答案:

答案 0 :(得分:2)

选项-1:非常整洁,但由于嵌套EXISTS子句可能效率最高:

q = (db.session.query(Recipe)
     .filter(Recipe.ingredients.any(
         ModifiedIngredient.ingredients.any(
             Ingredient.name == 'onion')
     )))

选项-2:应该更快,但如果您只查询某些列(使用query(Recipe.name, ..)而不是如下所示的整个对象),则每个{最终会有多个结果{因Recipe

而排{1}}行
JOINs