Node.js order by子句中不存在列

Node.js order by子句中不存在列,node.js,sequelize.js,Node.js,Sequelize.js,我使用Sequelize查询到如下表: const students = await User.findAll({ attributes: ['id', [Sequelize.literal(`"firstName" || ' ' || "lastName"`), 'name']], where: { [Op.or]: [ { firstName: { [Op.iLike]: `%${search}%` } }, {

我使用Sequelize查询到如下表:

  const students = await User.findAll({
attributes: ['id', [Sequelize.literal(`"firstName" || ' ' || "lastName"`), 'name']],
where: {
  [Op.or]: [
    {
      firstName: {
        [Op.iLike]: `%${search}%`
      }
    },
    {
      lastName: {
        [Op.iLike]: `%${search}%`
      }
    },
    Sequelize.literal(`"firstName" || ' ' || "lastName" ILIKE '%${search}%'`)
  ]
},
order: Sequelize.literal(`"organization_users"."firstName" ${NATURAL_SORT} ${sort}`),
limit,
offset: limit * (page - 1),
distinct: true
});
工作正常,但如果我包括一些模型,“firstName”列不存在:

  const students = await User.findAll({
attributes: ['id', [Sequelize.literal(`"firstName" || ' ' || "lastName"`), 'name']],
where: {
  [Op.or]: [
    {
      firstName: {
        [Op.iLike]: `%${search}%`
      }
    },
    {
      lastName: {
        [Op.iLike]: `%${search}%`
      }
    },
    Sequelize.literal(`"firstName" || ' ' || "lastName" ILIKE '%${search}%'`)
  ]
},
include: [
  { model: ProgressGrade, as: 'student_progress_grade', required: true, where: { courseId } },
  {
    model: CourseRole,
    as: 'course_roles',
    attributes: [],
    where: { roleName: 'Student' },
    required: true
  },
  { model: Course, as: 'courses', attributes: [], where: { id: courseId }, required: true }
],
order: Sequelize.literal(`"organization_users"."firstName" ${NATURAL_SORT} ${sort}`),
limit,
offset: limit * (page - 1),
distinct: true
});
错误消息:

错误:SequelizeDatabaseError:列组织_users.firstName 不存在

这是我第一次遇到这个问题,我做了很多搜索,但没有希望。有人知道Sequelize是如何工作的吗


我使用的是在Nodejs 13.8.0、PostgreSQL 10.12上运行的Sequelize 5.21.5,请将
子查询
属性设置为false,如下所示-

 const students = await User.findAll({
attributes: ['id', [Sequelize.literal(`"firstName" || ' ' || "lastName"`), 'name']],
where: {
  [Op.or]: [
    {
      firstName: {
        [Op.iLike]: `%${search}%`
      }
    },
    {
      lastName: {
        [Op.iLike]: `%${search}%`
      }
    },
    Sequelize.literal(`"firstName" || ' ' || "lastName" ILIKE '%${search}%'`)
  ]
},
include: [
  { model: ProgressGrade, as: 'student_progress_grade', required: true, where: { courseId } },
  {
    model: CourseRole,
    as: 'course_roles',
    attributes: [],
    where: { roleName: 'Student' },
    required: true
  },
  { model: Course, as: 'courses', attributes: [], where: { id: courseId }, required: true }
],
order: Sequelize.literal(`"organization_users"."firstName" ${NATURAL_SORT} ${sort}`),
limit,
offset: limit * (page - 1),
distinct: true,
subQuery: false
});

我希望有帮助

请在此分享准确的错误。请发布Sequelize生成的上一个查询及其输出、修改后生成的查询以及相应的输出?通过在属性中添加“firstName”,这将有助于理解我发现的问题list@NguyenHoangVu-K11FUGHCM,很高兴知道这一点!