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

fix: count just the right number of row #304

Merged
merged 3 commits into from Jan 17, 2022

Conversation

DenisFerrero
Copy link

Apply a more explicit count of data for the pagination, normally there's no problem but when doing an include in the default scope will be counted the included data for each record. For example:

By having those two models

let User = sequelize.define("users",
    {
      id: { type: DataTypes.INTEGER, autoIncrement: true, primaryKey: true },
      username: { type: DataTypes.STRING }
    }, { paranoid: true }
);
let Auth = sequelize.define("auths",
    {
      id: { type: DataTypes.INTEGER, autoIncrement: true, primaryKey: true },
      name: { type: DataTypes.STRING }
    }, { paranoid: false }
);

and they have a relation like this

Auth.belongsTo(models["users"], {
  foreignKey: "user_id",
  as: "users"
});

When trying to get all users with this defaultScope

User.associate = function (models) {
    User.addScope("defaultScope", {
      include: [{
        model: models["auths"],
        as: "auths"
      }]
    });
};

The count value is equal to the number of auths that are related to each user returned.

See this SandBox for a working sample.

Fix for #302

Copy link
Member

@icebob icebob left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thanks. Please add relevant tests as well.

@DenisFerrero
Copy link
Author

I'm not really into tests but I did my best to understand how to do that. Is it okay or do I need to explicit something else?

Copy link
Member

@icebob icebob left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, it works with older sequelize versions, as well?

@DenisFerrero
Copy link
Author

Don't tested, but I found from the Sequelize docs that the feature that I have applied ({ distinct: true, col: colName }) is available since Sequelize v3. Below the links to the docs of each version:

They wrote that if an include is provided this logic for filter the PK should be automatically applied but I think they didn't have tested using a defaulScope yet

@icebob
Copy link
Member

icebob commented Jan 17, 2022

Thank for your investigation.

@icebob icebob merged commit 024e740 into moleculerjs:master Jan 17, 2022
DenisFerrero pushed a commit to DenisFerrero/moleculer-db that referenced this pull request Jan 11, 2024
icebob added a commit that referenced this pull request Feb 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants