Issue Using sequelize.col with findAndCountAll and include in Sequelize with Join Table

Description:

I’m encountering an issue in a Sequelize project when trying to use sequelize.col() to select fields from associated models. The setup involves two main models, Student and User, where Student has a createdBy field referencing a User. Additionally, Student has a many-to-many relationship with StudyYear through a join table called StudentStudyYears.

My goal is to fetch the name of the creator (User.name) and alias it as creatorName during a findAndCountAll query. However, I’m getting an “unknown column” error when including the studyYears association.

Models and Associations

Student Model:

const Student = sequelize.define('Student', {
  id: {
    type: DataTypes.UUID,
    primaryKey: true,
  },
  code: DataTypes.STRING,
  name: DataTypes.STRING,
  lastname: DataTypes.STRING,
  createdBy: {
    type: DataTypes.UUID,
    allowNull: true,
  },
  // other attributes...
});

Student.belongsTo(User, { foreignKey: 'createdBy', as: 'creator' });
Student.belongsToMany(StudyYear, {
  through: StudentStudyYears,
  foreignKey: 'studentId',
  as: 'studyYears',
});

User Model:

const User = sequelize.define('User', {
  id: {
    type: DataTypes.UUID,
    primaryKey: true,
  },
  name: DataTypes.STRING,
  // other attributes...
});

StudyYear Model:

const StudyYear = sequelize.define('StudyYear', {
  id: {
    type: DataTypes.UUID,
    primaryKey: true,
  },
  year: DataTypes.STRING,
  // other attributes...
});

StudyYear.belongsToMany(Student, {
  through: StudentStudyYears,
  foreignKey: 'studyYearId',
  as: 'students',
});

Query

Here’s the query I’m trying to execute to fetch students along with specific creator data and study years:

const { count, rows } = await Student.findAndCountAll({
  attributes: [
    'id',
    'code',
    'name',
    'lastname',
    'avatar',
    'email',
    'dni',
    'phone',
    'dateOfBirth',
    'gender',
    'address',
    'isActive',
    'isBanned',
    [sequelize.col('creator.name'), 'creatorName'],
  ],
  include: [
    {
      model: User,
      as: 'creator',
      attributes: ['name', 'avatar'],
    },
    {
      model: StudyYear,
      as: 'studyYears',
      attributes: ['id', 'year'],
      through: { attributes: [] },
    },
  ],
  paranoid: true,
  where: {},
  order: [],
  offset: 0,
  limit: 10,
});

Error

The following error occurs when the query is executed:

(conn:488, no: 1054, SQLState: 42S22) Unknown column 'creator.name' in 'field list'

This error only appears when studyYears is included in the query. If I remove the studyYears part from the include array, the query runs successfully and returns the expected results.

What I’ve Tried

  • Confirming that associations are correctly defined.
  • Ensuring the columns and aliases in the query are correct.
  • Verifying that the tables and columns exist in the database and that relationships are properly set up.

Question

Why does sequelize.col('creator.name') fail to find the column ‘creator.name’ when studyYears is included? What might be causing this conflict, and how can I successfully fetch the creator’s name along with the study years in the same query?

Any help or guidance would be greatly appreciated!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật