如何在烧瓶sqlalchemy查询中获得差异,然后订购查询
我仍在尝试找出查询。我能够进行两个查询,现在我试图弄清楚如何结合它们。对于型号,我有一个类成分(db.model)
。在我的视图中
i0 = db.session.query(Ingredient).filter(Ingredient.ROrder.isnot(None),
Ingredient.recipe_id==0).order_by(Ingredient.ROrder)
ir = db.session.query(Ingredient).filter(Ingredient.ROrder.isnot(None),
Ingredient.recipe_id==recipe_id).order_by(Ingredient.ROrder)
。如果IR中存在source_id,则我不希望i0中的source_id实例。我希望这是有道理的。
编辑以添加表图像,
表就像。
source_id | name | Descriptor | tv | ROrder | recipe_id |
---|---|---|---|---|---|
3110 | Baking Powder | 61 | 0 | ||
C100 | Flour | All-purpose | 1781 | 362 | 0 |
C100 | Flour | Bread | 1346 | 207 | 1 |
3120 | Baking Soda | 83 | 0 | ||
B121 | Brown Sugar | Light | 542 | 41 | 1 |
B121 | Brown Sugar | Dark | 97 | 18 | 0 |
All records have source_id and recipe_id 。
I am still trying to figure out queries. I was able to make two queries and now I am trying to figure out how to combine them. For the models.py I have a class Ingredient(db.Model)
. In my views.py I have the queries:
i0 = db.session.query(Ingredient).filter(Ingredient.ROrder.isnot(None),
Ingredient.recipe_id==0).order_by(Ingredient.ROrder)
ir = db.session.query(Ingredient).filter(Ingredient.ROrder.isnot(None),
Ingredient.recipe_id==recipe_id).order_by(Ingredient.ROrder)
Okay so there is another column and I want to know the source_id. If source_id is present in the ir then I don't want the instance of source_id in i0. I hope this makes sense.
Edit to add table image
The table is like so.
source_id | name | Descriptor | tv | ROrder | recipe_id |
---|---|---|---|---|---|
3110 | Baking Powder | 61 | 0 | ||
C100 | Flour | All-purpose | 1781 | 362 | 0 |
C100 | Flour | Bread | 1346 | 207 | 1 |
3120 | Baking Soda | 83 | 0 | ||
B121 | Brown Sugar | Light | 542 | 41 | 1 |
B121 | Brown Sugar | Dark | 97 | 18 | 0 |
All records have source_id and recipe_id.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来您应该使用一个查询。如果这不是您寻求的解决方案,请更远地解释您要抓住的条件。
编辑:
我没有在本地运行此操作,所以我可能会有语法错误,但我可能会做这样的事情。只需将所有数据保存到字典中,当您获得已经存在的源_ID时,请覆盖值,如果repipe_id == repipe_id。
It sounds like you should be using a single query. If this is not the solution you seek, please farther explain the conditions you're trying to grab.
EDIT:
I didn't run this locally so I may have syntax error but I would likely do something like this. Just save all the data into a dictionary and when you get a source_id that already exist, overwrite the values if recipe_id==recipe_id.