以下是sequelize (6.3.5)中使用模型Artwork连接Artimage的查询
const { Artwork, validateArtwork} = require('../models/artwork');
const { Artimage, validateArtimage} = require('../models/artimage');
artworks = await Artwork.findAll({
where : {
id: {[Op.gt]:last_artwork_id},
},
include: [{
model:Artimage,
attributes:['width', 'height', ['name', 'img_name'], 'path', 'size_kb', ['label', 'img_label'], ['fileName', 'img_fileName']],
}],
attributes:{excludes:['fort_token']},
order:[['id', 'DESC']]
});这里,当last_artwork_id为7时,将在artimage中选取artwork#8及其2个图像。但是,artworks.length是1而不是2。但是,当执行原始SQL代码时,它返回2个条目,因为artwork#8有2个图像。下面是上面代码中的原始SQL:
SELECT "artwork"."id", "artwork"."name", "artwork"."auther", "artwork"."category", "artwork"."wt_g", "artwork"."production_year", "artwork"."dimension",
"artwork"."uploader_id", "artwork"."description", "artwork"."note", "artwork"."tag", "artwork"."deleted", "artwork"."status", "artwork"."artwork_data",
"artwork"."last_updated_by_id", "artwork"."fort_token", "artwork"."createdAt", "artwork"."updatedAt", "artimages"."id" AS "artimages.id",
"artimages"."width" AS "artimages.width", "artimages"."height" AS "artimages.height", "artimages"."name" AS "artimages.img_name", "artimages"."path" AS "artimages.path",
"artimages"."size_kb" AS "artimages.size_kb", "artimages"."label" AS "artimages.img_label", "artimages"."fileName" AS "artimages.img_fileName" FROM
"artworks" AS "artwork" LEFT OUTER JOIN "artimages" AS "artimages" ON "artwork"."id" = "artimages"."artwork_id" WHERE "artwork"."id" > 7 ORDER BY "artwork"."id" DESC为什么带有sequelizejs的artworks.length是1个项目,而不是2个项目(正确)?
发布于 2020-10-06 10:26:12
sequelizejs在具有表名为artimages:artworks.artimages[0] & artworks.artimages[1]的键的数组对象中返回联接的artimage
https://stackoverflow.com/questions/64179040
复制相似问题