蒸气流利或关系

问题描述 投票:0回答:1

我在 Vapor 中有两个模型,使用 Fluent 作为 ORM,一个是

Transaction
,另一个是
Invoice
Transaction
型号具有以下特点:

@OptionalParent(key: "invoice_id")
var invoice: Invoice?

因此

invoice
属性可以为零或分配给
Invoice
模型。我的问题是如何查询
Transaction
,其中
invoice
可以是
nil
OR 可以分配给
Invoice
,我可以在其中检查
invoice
是否处于活动状态(我有一个
 
isActive
模型上的Invoice
属性)?

我尝试阅读以下内容:

https://docs.vapor.codes/ Fluent/query/

https://forums.swift.org/t/vapor- Fluent-query-support-and-xor/32647

到目前为止,我正在尝试这样做:

try? await Transaction.query(on: request.db)
    .with(\.$invoice)
    .sort(\.$dateCreated, .descending)
    .all()

然后我循环遍历所有

transaction
,并跳过未将
isActive
设置为
true
的内容。

我还尝试了以下方法:

    .filter(\.$isActive == false)
    .all()) ?? []
        
    let transactions = (try? await Transaction.query(on: request.db)
        .with(\.$invoice)
        .group(.or) { group in
            group
                .filter(\Transaction.$invoice.$id !~ (try inactiveInvoices.map { try $0.requireID() }))
                .filter(\Transaction.$invoice.$id == nil)
        }
        .sort(\.$dateCreated, .descending)
        .all()) ?? []

它有效......但其中有很多疑问。有没有办法简化一下?

我基本上想检查它是否是

nil
或者
isActive
上的
invoice
属性是否是查询本身的
true
。这在 Vapor 中可能吗?如有任何帮助,我们将不胜感激!

fluent vapor
1个回答
0
投票

您可以在原始查询的过滤器中执行此操作,位于

with
:

之后
try? await Transaction.query(on: request.db)
        .with(\.$invoice)
        .filter(\.$invoice.$isActive == true)
        .sort(\.$dateCreated, .descending)
        .all()

我感觉这里需要看似多余的

== true
,但目前无法检查。

© www.soinside.com 2019 - 2024. All rights reserved.