簡體   English   中英

Sequelize - 使用 hasMany 和 belongsToMany 關聯無法正確限制包含的查詢

[英]Sequelize - Can't limit properly query with includes, using hasMany and belongsToMany associations

問題說明

我想做一個分頁查詢,每個查詢限制為 12 行,更具體地說,每個查詢限制為 12 個Batches 實際上,行數變小了,因為我在此查詢中獲得了與連接表的belongsToMany關聯。 此查詢的連接順序是: Offer > hasMany > Batch > belongsToMany > BatchFile > belongsTo > File 問題是,當我在File as 'gallery'關聯時,它會給我帶來重復的批處理注冊表。

我正在嘗試做的查詢

const { id } = req.params;
const { page = 1 } = req.query;

const offer = await Offer.findAndCountAll({
  attributes: [ 'id', 'name', 'canceled_at'],
  where: { id, canceled_at: null },
  order: [['offer_batches', 'name', 'ASC']],
  include: [
    {
      /* hasMany association */
      model: Batch,
      as: 'offer_batches',
      attributes: [ 'id', 'name'],
      include: [
        { /* Other includes... */ },
        {
          /* belongsToMany association */
          model: File,
          as: 'gallery',
          attributes: ['id', 'path', 'url'],
        },
      ],
    },
  ],
  subQuery: false,
  limit: 12,
  offset: (page - 1) * 12,
});

Model 關聯

報價 model

this.hasMany(Batch, { foreignKey: 'offer_id', as: 'offer_batches' });

批次 model

this.belongsToMany(File, { through: models.BatchFile, as: 'gallery' });

批處理文件 model(連接表)

this.belongsTo(Batch, { foreignKey: 'batch_id', as: 'batch' });
this.belongsTo(File, { foreignKey: 'file_id', as: 'file' });

我已經嘗試過的

為任何包含的 Model 提供duplicating: false選項不起作用;

separate: true Batch model 也不起作用;

Give required: true option to any include model 也不起作用;

如果我刪除subQuery: false它不遵守設置的行數限制,並且我已經嘗試了上述所有組合;

我認為 sequelize 可以毫無問題地處理這種情況,也許我做錯了什么。

如果有幫助,這里是原始生成的 SQL:

SELECT
"Offer"."id",
"Offer"."name",
"Offer"."canceled_at",
"offer_batches"."id"
AS "offer_batches.id", "offer_batches"."name"
AS "offer_batches.name", "offer_batches->gallery"."id"
AS "offer_batches.gallery.id", "offer_batches->gallery"."path"
AS "offer_batches.gallery.path", "offer_batches->gallery->BatchFile"."created_at"
AS "offer_batches.gallery.BatchFile.createdAt", "offer_batches->gallery->BatchFile"."updated_at"
AS "offer_batches.gallery.BatchFile.updatedAt", "offer_batches->gallery->BatchFile"."file_id"
AS "offer_batches.gallery.BatchFile.FileId", "offer_batches->gallery->BatchFile"."batch_id"
AS "offer_batches.gallery.BatchFile.BatchId", "offer_batches->gallery->BatchFile"."batch_id"
AS "offer_batches.gallery.BatchFile.batch_id", "offer_batches->gallery->BatchFile"."file_id"
AS "offer_batches.gallery.BatchFile.file_id"
FROM "offer" AS "Offer"
LEFT OUTER JOIN "batch" AS "offer_batches" ON "Offer"."id" = "offer_batches"."offer_id"
LEFT OUTER JOIN (
  "batch_file" AS "offer_batches->gallery->BatchFile"
  INNER JOIN "file" AS "offer_batches->gallery"
  ON "offer_batches->gallery"."id" = "offer_batches->gallery->BatchFile"."file_id"
)
ON "offer_batches"."id" = "offer_batches->gallery->BatchFile"."batch_id"
WHERE "Offer"."id" = '1' AND "Offer"."canceled_at" IS NULL
ORDER BY "offer_batches"."name"
ASC LIMIT 12 OFFSET 0;

環境

節點: v14.18.0

package.json依賴項

  • 皮克: 8.7.1
  • pg-hstore: 2.3.4
  • 續集: 6.9.0

試圖找到有關 GitHub 問題或 stackoverflow 的任何解決方案,但沒有解決這個問題。 也許我做錯了這個查詢,任何幫助將不勝感激和歡迎:)

好吧,我沒有找到一個很好的解決方案,所以我決定在這種情況下使用Lazy loading而不是Eager loading ,如Sequelize Docs中所述。

我將查詢拆分為兩個新查詢。

第一個,在“主” model Offer上,使用簡單的findOne()

const offer = await Offer.findOne({
  [/* My attributes */],
  where: { /* My conditions */ }
});

第二個,從 model Batch中選擇,沒有subQuery: false選項,因為不再需要。

const batches = await Batch.findAndCountAll({
  attributes: [
    'id',
    'name',
  ],
  where: { offer_id: offer.id },
  order: [/* My ordenation */],
  include: [
    {
      model: File,
      as: 'gallery',
      attributes: ['id', 'path', 'url'],
    },
  ],
  limit: 12,
  offset: (page - 1) * 12,
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM