删除 sequelize 返回值中的 join table 数据

Removing join table data in sequelize returned value

我目前正在尝试删除检索关联数据时添加的 joint table 数据。

查询是通过 sequelize 使用通过指定模型关系添加到模型的方法完成的(sequelize magic methods),出于某种原因,我无法做到这一点。

我目前已尝试将 attributes: {exclude: ['...']} 传递给该方法,但该字段仍然存在。

当前关联

// Class sequelize model

Class.belongsToMany(models.Subject, { 
  through: 'ClassSubject', 
  foreignKey: 'class_id', 
  otherKey: 'subject_id', 
  as: 'subjects' 
})


// Subject sequelize model

Subject.belongsToMany(models.Class, { 
  through: 'ClassSubject', 
  foreignKey: 'subject_id', 
  otherKey: 'class_id',
  as: 'classes' 
});

查询和响应

const subjects = await dbClass.getSubjects(); // dbClass is a Class model instance

// Response
[
  {
    "id": "1b89d44c-2caa-452d-a1f8-7faa11970917",
    "name": "Mathematics",
    "code": "MATHS",
    "summary": "Mathematics for class 1",
    "ClassSubject": {
      "class_id": "637afc7b-40f6-478e-b35e-859ca462e2e7",
      "subject_id": "1b89d44c-2caa-452d-a1f8-7faa11970917"
    }
  }
]

期望输出

// Response
[
  {
    "id": "1b89d44c-2caa-452d-a1f8-7faa11970917",
    "name": "Mathematics",
    "code": "MATHS",
    "summary": "Mathematics for class 1"
  }
]

我已尝试将选项传递给下面指定的方法,但无济于事

const subjects = await dbClass.getSubjects({ 
    attributes: { exclude: ['ClassSubject'] } 
});

但是还是不行。

尝试使用 joinTableAttributes 选项并传递空数组以排除联合 table 中的所有内容。

const subjects = await dbClass.getSubjects({ joinTableAttributes: [] });