[英]Loopback join relation without selecting relation
我正在嘗試使用 loopback 4 構建一個具有 2 個實體之間關系的查詢
客戶.model.ts:
@model()
export class Customer extends Entity {
// id, name properties
@hasMany(() => Order)
orders?: Order[];
}
order.model.ts:
@model()
export class Order extends Entity {
// id, desc properties
@belongsTo(() => Customer)
customerId: Customer;
}
我的目標是讓所有至少有 1 個訂單但沒有選擇他們的訂單的客戶,這是我的查詢
await customerRepository.find({
include: [{ relation: "orders" }],
});
我也試過:
await customerRepository.find({
include: [{ relation: "orders" }],
fields: {propertyName: }
});
謝謝您的幫助!
使用 INNER JOIN 可以排除沒有任何相關數據的條目(在您的情況下排除沒有任何訂單的客戶),目前默認情況下在環回中不支持這種行為。
您可以使用我最近開發的這個新發布的 sequelize 擴展,稱為loopback4-sequelize ,它也支持內部連接。
為了達到預期的結果,您需要按照此處所述將required
設置為true
,因此您的過濾器 object 將如下所示:
{
"include": [{
"relation": "orders",
"required": true, // returns only those customers who have orders
"scope": {
"fields": [] // exclude all fields in orders
}
}]
}
要記住的一點是,默認情況下環回從不在內部運行 SQL JOIN 查詢。 它使用負責包含相關模型的包含解析器,並在返回源表的數據后調用。 當您在過濾器 object 中使用include
時,會導致兩個不同的調用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.