如何在烧瓶sqlalchemy查询中获得差异,然后订购查询

发布于 2025-02-09 03:36:35 字数 1303 浏览 3 评论 0原文

我仍在尝试找出查询。我能够进行两个查询,现在我试图弄清楚如何结合它们。对于型号,我有一个类成分(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_idnameDescriptortvROrderrecipe_id
3110Baking Powder610
C100FlourAll-purpose17813620
C100FlourBread13462071
3120Baking Soda830
B121Brown SugarLight542411
B121Brown SugarDark97180

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_idnameDescriptortvROrderrecipe_id
3110Baking Powder610
C100FlourAll-purpose17813620
C100FlourBread13462071
3120Baking Soda830
B121Brown SugarLight542411
B121Brown SugarDark97180

All records have source_id and recipe_id.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玻璃人 2025-02-16 03:36:35
combined = db.session.query(Ingredient).filter(Ingredient.ROrder.isnot(None)).\ 
    filter(_or(Ingredient.recipe_id==0, Ingredient.recipe_id==recipe_id)).\
    order_by(Ingredient.ROrder)

听起来您应该使用一个查询。如果这不是您寻求的解决方案,请更远地解释您要抓住的条件。

编辑:
我没有在本地运行此操作,所以我可能会有语法错误,但我可能会做这样的事情。只需将所有数据保存到字典中,当您获得已经存在的源_ID时,请覆盖值,如果repipe_id == repipe_id。

combined_objects = db.session.query(Ingredient).filter(Ingredient.ROrder.isnot(None)).\ 
    filter(_or(Ingredient.recipe_id==0, Ingredient.recipe_id==recipe_id)).\
    order_by(Ingredient.ROrder).all()

data_dict = {}
source_id_list = []

for combined_object in combined_objects:
    if combined_object.source_id not in data_dict.keys():
        # add to dictionary
        temp_dict = {'name': combined_object.name, 'Descriptor': combined_object.Descriptor,
             'tv': combined_object.tv, 'ROrder': combined_object.ROrder, 'recipe_id': combined_object.recipe_id}
        data_dict[combined_object.source_id] = temp_dict
        # keep track of list of source_ids
        source_id_list.append(combined_object.source_id)
    
    # if key is already in dictionary, check if recipe_id==recipe_id, if it does, overwrite data in dictionary
    else:
        if combined_object.recipe_id == recipe_id:
            # add to dictionary
            temp_dict = {'name': combined_object.name, 'Descriptor': combined_object.Descriptor,
                'tv': combined_object.tv, 'ROrder': combined_object.ROrder, 'recipe_id': combined_object.recipe_id}
            data_dict[combined_object.source_id] = temp_dict

# show result
for source_id in source_id_list:
    print(data_dict[source_id])
combined = db.session.query(Ingredient).filter(Ingredient.ROrder.isnot(None)).\ 
    filter(_or(Ingredient.recipe_id==0, Ingredient.recipe_id==recipe_id)).\
    order_by(Ingredient.ROrder)

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.

combined_objects = db.session.query(Ingredient).filter(Ingredient.ROrder.isnot(None)).\ 
    filter(_or(Ingredient.recipe_id==0, Ingredient.recipe_id==recipe_id)).\
    order_by(Ingredient.ROrder).all()

data_dict = {}
source_id_list = []

for combined_object in combined_objects:
    if combined_object.source_id not in data_dict.keys():
        # add to dictionary
        temp_dict = {'name': combined_object.name, 'Descriptor': combined_object.Descriptor,
             'tv': combined_object.tv, 'ROrder': combined_object.ROrder, 'recipe_id': combined_object.recipe_id}
        data_dict[combined_object.source_id] = temp_dict
        # keep track of list of source_ids
        source_id_list.append(combined_object.source_id)
    
    # if key is already in dictionary, check if recipe_id==recipe_id, if it does, overwrite data in dictionary
    else:
        if combined_object.recipe_id == recipe_id:
            # add to dictionary
            temp_dict = {'name': combined_object.name, 'Descriptor': combined_object.Descriptor,
                'tv': combined_object.tv, 'ROrder': combined_object.ROrder, 'recipe_id': combined_object.recipe_id}
            data_dict[combined_object.source_id] = temp_dict

# show result
for source_id in source_id_list:
    print(data_dict[source_id])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文