gpt4 book ai didi

sequelize.js - 为什么 sequelize 查询返回一项而原始 SQL 返回 2 项?

转载 作者:行者123 更新时间:2023-12-03 22:33:36 25 4
gpt4 key购买 nike

这是 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 时, artwork#8 将与 artimage 中的 2 个图像一起选取。然而 artworks.length1 而不是 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
为什么 artworks.length 是带有 sequelizejs 的 1 项而不是 2 项(正确)?

最佳答案

sequelizejs 在一个数组对象中返回连接的 artimage,其键为表名 artimages : artworks.artimages[0] & artworks.artimages[1]

关于sequelize.js - 为什么 sequelize 查询返回一项而原始 SQL 返回 2 项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64179040/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com