簡體   English   中英

使用 Sequelize 的 INSERT 和 UPDATE 查詢中的意外順序

[英]Unexpected order in INSERT and UPDATE queries with Sequelize

首先,我將簡要解釋我正在嘗試做什么以及相關的模型。

擁有一個 trackingIds 數組(10 個元素),使用這個 trackingId 和一個“免費”調色板創建一個染色體

考慮:

Project.hasMany(models.Palette);
Project.hasMany(models.Chromosome);
Chromosome.hasOne(models.Palette);
Palette.belongsTo(models.Project, {foreignKey: 'projectId', as: 'project'});
Palette.belongsTo(models.Chromosome, {foreignKey: 'chromosomeId', as: 'chromosome'});

現在,我的代碼看起來像這樣:

freeTrackingIds.forEach(async (trackingId) => {
    // Since the project has many palettes, I want to assign to the chromosome one palette that is free (this means, that has not been assigned to any chromosome yet).
    // I tried to reload() the project to fetch the changes in its palettes in previous iterations
    const availablePalettes = (await project.reload()).palettes.filter((palette) => !palette.chromosomeId);

    // Choosing a random palette from my available palettes. Yes, there might be better ways to achieve this. 
    const randomPalette = availablePalettes[Math.floor(Math.random() * availablePalettes.length)];

    // Creating the new chromosome for the project with the trackingId 
    const chromosome = await Chromosome.create({ projectId: project.id, trackingId: trackingId });

    // Linking the Palette to the chromosome
    randomPalette.chromosomeId = chromosome.id;
    await randomPalette.save();
  });

我注意到, availablePalettes語句會檢索我在此 forEach 的先前迭代中分配的調色板(這就是我決定每次都重新加載項目實體的原因)。

即使重新加載項目(返回其免費可用的調色板)我也經歷過這一點。 檢查我的日志我注意到:

Executing (default): INSERT INTO "Chromosomes" ("id","trackingId","elements","timesRequested","generation","createdAt","updatedAt","projectId") VALUES (DEFAULT,$1,$2,$3,$4,$5,$6,$7) RETURNING *;
Executing (default): INSERT INTO "Chromosomes" ("id","trackingId","elements","timesRequested","generation","createdAt","updatedAt","projectId") VALUES (DEFAULT,$1,$2,$3,$4,$5,$6,$7) RETURNING *;
Executing (default): INSERT INTO "Chromosomes" ("id","trackingId","elements","timesRequested","generation","createdAt","updatedAt","projectId") VALUES (DEFAULT,$1,$2,$3,$4,$5,$6,$7) RETURNING *;
Executing (default): INSERT INTO "Chromosomes" ("id","trackingId","elements","timesRequested","generation","createdAt","updatedAt","projectId") VALUES (DEFAULT,$1,$2,$3,$4,$5,$6,$7) RETURNING *;
Executing (default): INSERT INTO "Chromosomes" ("id","trackingId","elements","timesRequested","generation","createdAt","updatedAt","projectId") VALUES (DEFAULT,$1,$2,$3,$4,$5,$6,$7) RETURNING *;
Executing (default): INSERT INTO "Chromosomes" ("id","trackingId","elements","timesRequested","generation","createdAt","updatedAt","projectId") VALUES (DEFAULT,$1,$2,$3,$4,$5,$6,$7) RETURNING *;
Executing (default): INSERT INTO "Chromosomes" ("id","trackingId","elements","timesRequested","generation","createdAt","updatedAt","projectId") VALUES (DEFAULT,$1,$2,$3,$4,$5,$6,$7) RETURNING *;
Executing (default): INSERT INTO "Chromosomes" ("id","trackingId","elements","timesRequested","generation","createdAt","updatedAt","projectId") VALUES (DEFAULT,$1,$2,$3,$4,$5,$6,$7) RETURNING *;
Executing (default): INSERT INTO "Chromosomes" ("id","trackingId","elements","timesRequested","generation","createdAt","updatedAt","projectId") VALUES (DEFAULT,$1,$2,$3,$4,$5,$6,$7) RETURNING *;
Executing (default): UPDATE "Palettes" SET "chromosomeId"=$1,"updatedAt"=$2 WHERE "id" = $3
Executing (default): UPDATE "Palettes" SET "chromosomeId"=$1,"updatedAt"=$2 WHERE "id" = $3
Executing (default): UPDATE "Palettes" SET "chromosomeId"=$1,"updatedAt"=$2 WHERE "id" = $3
Executing (default): UPDATE "Palettes" SET "chromosomeId"=$1,"updatedAt"=$2 WHERE "id" = $3
Executing (default): UPDATE "Palettes" SET "chromosomeId"=$1,"updatedAt"=$2 WHERE "id" = $3
Executing (default): UPDATE "Palettes" SET "chromosomeId"=$1,"updatedAt"=$2 WHERE "id" = $3
Executing (default): UPDATE "Palettes" SET "chromosomeId"=$1,"updatedAt"=$2 WHERE "id" = $3
Executing (default): UPDATE "Palettes" SET "chromosomeId"=$1,"updatedAt"=$2 WHERE "id" = $3
Executing (default): UPDATE "Palettes" SET "chromosomeId"=$1,"updatedAt"=$2 WHERE "id" = $3

我不確定這是否完全基於時間,但看起來 sequelize(當然是由於性能原因)在分配托盤之前完成了我所有的染色體創建。 這可能是為什么 availablePalettes 返回在此 forEach() 的先前迭代中分配給 Chromosomes 的調色板的原因嗎?

暫時就這些了,先謝謝了!

數組 object 的 forEach 函數不支持異步語法。 使用“為”:

forof (const trackingId of freeTrackingIds) {
// Since the project has many palettes, I want to assign to the chromosome one palette that is free (this means, that has not been assigned to any chromosome yet).
    // I tried to reload() the project to fetch the changes in its palettes in previous iterations
    const availablePalettes = (await project.reload()).palettes.filter((palette) => !palette.chromosomeId);

    // Choosing a random palette from my available palettes. Yes, there might be better ways to achieve this. 
    const randomPalette = availablePalettes[Math.floor(Math.random() * availablePalettes.length)];

    // Creating the new chromosome for the project with the trackingId 
    const chromosome = await Chromosome.create({ projectId: project.id, trackingId: trackingId });

    // Linking the Palette to the chromosome
    randomPalette.chromosomeId = chromosome.id;
    await randomPalette.save();  
}

我還建議您將事務用於此類批處理操作。

暫無
暫無

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

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