Umbrella
Umbrella

Reputation: 1145

Get all the rows that have unique values in a field, using sequelize

What options should I pass to findAndCountAll method of a sequelize model so that it returns the rows that have unique values in a certain field? In my particular case I need to get all the models that have unique attack_id field values.

const { count, rows } = await myModel.findAndCountAll(?????);

Upvotes: 0

Views: 1575

Answers (1)

Umbrella
Umbrella

Reputation: 1145

const { count, rows } = await myModel.findAndCountAll({
attributes: [
        [sequelize.fn('DISTINCT', sequelize.col('attack_id')), 'attack_id'],
      ],
});

That's how I did it. But the count is incorrect, as it doesn't count the unique property.

Upvotes: 2

Related Questions