Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retrieving all data from a junction table without specifying it on the model query #16655

Open
2 tasks done
augusto-dmh opened this issue Oct 18, 2023 · 0 comments
Open
2 tasks done
Labels
pending-approval Bug reports that have not been verified yet, or feature requests that have not been accepted yet type: bug

Comments

@augusto-dmh
Copy link

Issue Creation Checklist

  • I understand that my issue will be automatically closed if I don't fill in the requested information
  • I have read the contribution guidelines

Bug Description

The problem is related to the way sequelize tries to retrieve data from a table related to other table through a junction table, but retrieves also all the data from the junction table itself. Specifying this to the case i noticed this, i'm trying to get data from a table "modalities" that is related to "courses" through a join table (one created by myself using sequelize migrations as well as all the others on my db) "course_modalities". Here is my sequelize model query and my models Course, Modality, CourseModality:

Sequelize model query:

let courses = await Course.findAll({
      include: { model: Modality, attributes: ["name"] },
    });

Course Model:

export default class Course extends Model {
  static init(sequelize) {
    super.init(
      {
        name: {
          type: DataTypes.STRING,
        },
        category: {
          type: DataTypes.STRING,
        },
        durationSem: {
          type: DataTypes.INTEGER,
        },
        degree: {
          type: DataTypes.STRING,
        },
      },
      { sequelize },
    );

    return this;
  }

  static associate(models) {
    this.hasMany(models.Student, { foreignKey: "course_id" });
    this.hasMany(models.Professor, { foreignKey: "course_id" });
    this.belongsToMany(models.Modality, { through: models.CourseModality });
  }
}

Modality model:

export default class Modality extends Model {
  static init(sequelize) {
    super.init(
      {
        name: {
          type: DataTypes.STRING,
          allowNull: false,
        },
      },
      { sequelize },
    );

    return this;
  }

  static associate(models) {
    this.belongsToMany(models.Course, { through: models.CourseModality });
  }
}

CourseModality model

export default class CourseModality extends Model {
  static init(sequelize) {
    super.init(
      {
        course_id: {
          type: DataTypes.INTEGER,
          allowNull: false,
        },
        modality_id: {
          type: DataTypes.INTEGER,
          allowNull: false,
        },
      },
      { sequelize, timestamps: false },
    );

    return this;
  }
}

But by testing on routes, the result that i get is quite weird: i'm getting the attributes i want from "Modality" but not just that, i'm getting all the attributes from the junction table (even duplicates on camelCase, even if on my db the attributes are defined in snake_case) without specifying nothing about it. Example of one retrieved data:

{
    "id": 1,
    "name": "Arquitetura e Urbanismo",
    "category": "Arquitetura e Urbanismo & Design",
    "durationSem": 10,
    "degree": "Bachelor",
    "createdAt": "2023-10-18T03:05:41.000Z",
    "updatedAt": "2023-10-18T03:05:41.000Z",
    "Modalities": [
        {
            "name": "On-campus",
            "CourseModality": {
                "course_id": 1,
                "modality_id": 1,
                "CourseId": 1,
                "ModalityId": 1
            }
        }
    ]
},

The output i'm trying to get:

{
    "id": 1,
    "name": "Arquitetura e Urbanismo",
    "category": "Arquitetura e Urbanismo & Design",
    "durationSem": 10,
    "degree": "Bachelor",
    "createdAt": "2023-10-18T03:05:41.000Z",
    "updatedAt": "2023-10-18T03:05:41.000Z",
    "Modalities": [
        {
            "name": "On-campus"
        }
    ]
},

I'm using Sequelize v6 with MYSQL as dialect.


Indicate your interest in the resolution of this issue by adding the 👍 reaction. Comments such as "+1" will be removed.

@augusto-dmh augusto-dmh added pending-approval Bug reports that have not been verified yet, or feature requests that have not been accepted yet type: bug labels Oct 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pending-approval Bug reports that have not been verified yet, or feature requests that have not been accepted yet type: bug
Projects
None yet
Development

No branches or pull requests

1 participant