SQLAlchemy - 过滤查询,排除 parent 其中许多 children 之一符合条件
SQLAlchemy - Filter query, exclude parent where one of many children meet criteria
我的 SQL 技能很差,所以我不知道如何形成我需要的查询。
我有两个具有一对多关系的数据库模型,定义如下:
class Parent(db.Model):
__tablename__ = 'parent'
id = db.Column(db.Integer, primary_key = True)
children = db.relationship('Child',
backref = 'parent',
lazy = 'joined')
class Child(db.Model):
__tablename__ = 'child'
id = db.Column(db.Integer, primary_key = True)
parent_id = db.Column(db.Integer, db.ForeignKey('parent.id'))
value = db.Column(db.String(140))
我希望能够形成一个查询,该查询将 return 所有满足三个条件的 parents:
1:有一个或多个children,其值包含'value1'
2:有一个或多个children,其值包含'value2'
3: 没有 children 的值包含 'value3' 或 'value4'
对于此示例数据:
Parents:
id |
1 |
2 |
3 |
4 |
Children:
id | parent_id | value
1 | 1 | 'value1'
2 | 1 | 'value2'
3 | 1 | 'value3'
4 | 1 | 'value5'
5 | 2 | 'value1'
6 | 2 | 'value2'
7 | 2 | 'value4'
8 | 2 | 'value5'
9 | 3 | 'value1'
10 | 3 | 'value2'
11 | 3 | 'value5'
12 | 3 | 'value6'
13 | 4 | 'value1'
14 | 4 | 'value7'
我只想 Parent #3 被 returned。
据我所知:
from sqlalchemy import not_, and_
conditions = []
conditions.append(Parent.children.any(Child.value.ilike('%'+value1+'%'))
conditions.append(Parent.children.any(Child.value.ilike('%'+value2+'%'))
conditions.append(Parent.children.any(not_(Child.value.ilike('%'+value3+'%')))
condition = and_(*conditions)
q = db.session.query(Parent).filter(condition)
前两个条件工作正常。将关系设置为 lazy='join' 允许我在关系上调用 .any() 并获得我想要的结果。
但是,条件 3 并没有按原样运行。是 returning Parent 有一个 child 不符合标准,而不是所有 children 不符合标准。
我搞砸了外连接和其他执行此查询的方法,但我意识到我对 SQL 的了解还不够,无法确定前进的方向。谁能指出我正确的方向?只知道我需要生成的 SQL 将是朝着正确方向迈出的一大步,但让它在 SQLAlchemy 中工作会很棒。
下面的查询应该可以做到:
q = (session.query(Parent)
.filter(Parent.children.any(Child.value.ilike('%{}%'.format(value1))))
.filter(Parent.children.any(Child.value.ilike('%{}%'.format(value2))))
.filter(~Parent.children.any(
db.or_(Child.value.ilike('%{}%'.format(value3)),
Child.value.ilike('%{}%'.format(value4)),
)
))
)
几点:
- 您需要
or
条件 3
- 你也应该使用
NOT has any children...
(这是使用 ~
完成的)或者你里面的 not
。
条件 3 的过滤器应该是:Parents 没有任何 children 满足 bla-bla,而你的代码暗示 Parents 至少有一个 child 不满足 bla-bla.
我的 SQL 技能很差,所以我不知道如何形成我需要的查询。
我有两个具有一对多关系的数据库模型,定义如下:
class Parent(db.Model):
__tablename__ = 'parent'
id = db.Column(db.Integer, primary_key = True)
children = db.relationship('Child',
backref = 'parent',
lazy = 'joined')
class Child(db.Model):
__tablename__ = 'child'
id = db.Column(db.Integer, primary_key = True)
parent_id = db.Column(db.Integer, db.ForeignKey('parent.id'))
value = db.Column(db.String(140))
我希望能够形成一个查询,该查询将 return 所有满足三个条件的 parents:
1:有一个或多个children,其值包含'value1'
2:有一个或多个children,其值包含'value2'
3: 没有 children 的值包含 'value3' 或 'value4'
对于此示例数据:
Parents:
id |
1 |
2 |
3 |
4 |
Children:
id | parent_id | value
1 | 1 | 'value1'
2 | 1 | 'value2'
3 | 1 | 'value3'
4 | 1 | 'value5'
5 | 2 | 'value1'
6 | 2 | 'value2'
7 | 2 | 'value4'
8 | 2 | 'value5'
9 | 3 | 'value1'
10 | 3 | 'value2'
11 | 3 | 'value5'
12 | 3 | 'value6'
13 | 4 | 'value1'
14 | 4 | 'value7'
我只想 Parent #3 被 returned。
据我所知:
from sqlalchemy import not_, and_
conditions = []
conditions.append(Parent.children.any(Child.value.ilike('%'+value1+'%'))
conditions.append(Parent.children.any(Child.value.ilike('%'+value2+'%'))
conditions.append(Parent.children.any(not_(Child.value.ilike('%'+value3+'%')))
condition = and_(*conditions)
q = db.session.query(Parent).filter(condition)
前两个条件工作正常。将关系设置为 lazy='join' 允许我在关系上调用 .any() 并获得我想要的结果。
但是,条件 3 并没有按原样运行。是 returning Parent 有一个 child 不符合标准,而不是所有 children 不符合标准。
我搞砸了外连接和其他执行此查询的方法,但我意识到我对 SQL 的了解还不够,无法确定前进的方向。谁能指出我正确的方向?只知道我需要生成的 SQL 将是朝着正确方向迈出的一大步,但让它在 SQLAlchemy 中工作会很棒。
下面的查询应该可以做到:
q = (session.query(Parent)
.filter(Parent.children.any(Child.value.ilike('%{}%'.format(value1))))
.filter(Parent.children.any(Child.value.ilike('%{}%'.format(value2))))
.filter(~Parent.children.any(
db.or_(Child.value.ilike('%{}%'.format(value3)),
Child.value.ilike('%{}%'.format(value4)),
)
))
)
几点:
- 您需要
or
条件 3 - 你也应该使用
NOT has any children...
(这是使用~
完成的)或者你里面的not
。
条件 3 的过滤器应该是:Parents 没有任何 children 满足 bla-bla,而你的代码暗示 Parents 至少有一个 child 不满足 bla-bla.