我如何在where子句中获取另一个表格行

时间:2019-05-25 08:56:53

标签: postgresql sequelize.js

我想从Table book和Table章节中获取数据,并希望将Table章节中的一行合并到Table book中。

我想将最大章节续写数的行添加到表中

nodejs 10.5 续订4.8.0

我的失败代码

 let result = await model.shelf.findAll({
    where: {
      userId: id
    },
    attributes: ['id', 'type', 'lastChapter', 'createTime'],
    include: [
      {
        model: model.book,
        attributes: {
          include: ['bookName', 'poster'],
          exclude: ['updateTime', 'createTime', 'site', 'source', 'summary']
        },
        as: 'book',
        include: [
          {
            model: model.chapter,
            attributes: {
              include: ['id', 'chapterName', 'createTime'],
              exclude: ['updateTime', 'site', 'source', 'words', 'content', 'bookId']
            },
            where: db.where(db.col('chapter.seq'), db.fn('max', db.col('chapter.seq'))),              // '$chapter.seq$': db.fn('max', db.col('chapter.seq'))

            as: 'chapter',
            // order: [
            //   [db.fn('max', db.col('chapter.seq')), 'DESC']
            // ],
          },
          {
            model: model.author,
            attributes: {
              include: ['name'],
              exclude: ['createTime', 'updateTime', 'introduce', 'level', 'avatar'],
            },
            as: 'author'
          }
        ]
      },
    ],
    order: ['updateTime'],
    group: ['id']
  });

我希望得到类似的数据

[ { ..attributes, book: { chapter: { ...(max seq of Chaper) }, author: { ...authorAttributes } } }, ... ]

但是发生了错误

err name: SequelizeDatabaseError err message: missing FROM-clause entry for table "chapter" err stack: SequelizeDatabaseError: missing FROM-clause entry for table "chapter"

0 个答案:

没有答案
相关问题