Repository navigation
Closed
Labels
Description
I have two models with m:n association:
class Model1 extends sequelize.Model {
static associate(models) {
Model1.belongsTo(models.OneToOneModel, { foreignKey: 'oneToOneId', as: 'oneToOne' });
Model1.belongsToMany(models.Model2, {
through: {
model: models.AssocModel,
unique: false,
},
foreignKey: 'model1Id',
constraints: false,
as: 'models2',
});
}
}
Model1.init({ /* code here */}, {
// code here
scopes: {
withOneToOne() {
return {
include: {
association: Model1.associations.oneToOne,
where: { field: 'value' },
require: true,
},
};
},
withModel2() {
return {
include: {
association: Model1.associations.models2,
},
};
},
},
}); class Model2 extends sequelize.Model {
static associate(models) {
Model2.belongsToMany(models.Model1, {
through: {
model: models.AssocModel,
unique: false,
},
foreignKey: 'model2Id',
constraints: false,
as: 'models1',
});
}
}I'm trying to make following query:
Model1.scope(['withOneToOne', 'withModel2']).findAndCountAll({
where: {},
order: [ [ 'createdAt', 'DESC' ],
limit: 20,
offset: 0 ,
distinct: true,
// distinct: 'Model1.id', I also tried this
// If i avoid `distinct` option,
// count will be wrong, due to `models2` different values with one `Model1`
});I'm expecting following query
SELECT count(DISTINCT (`Model1`.id)) AS `count`
FROM `Model1` AS `Model1` INNER JOIN `OneToOneModel` AS `oneToOne`
ON `Model1`.`oneToOnkeFk` = `oneToOne`.`id` AND `oneToOne`.`field` = 'value'
LEFT OUTER JOIN (`AssocModel` AS `models2->AssocModel` INNER JOIN `Model2` AS `models2`
ON `models2`.`id` = `models2->AssocModel`.`model2Id`)
ON `Model1`.`id` = `models2->AssocModel`.`model2Id`;But it gives me following query:
SELECT count(DISTINCT (*)) AS `count`
FROM `Model1` AS `Model1` INNER JOIN `OneToOneModel` AS `oneToOne`
ON `Model1`.`oneToOnkeFk` = `oneToOne`.`id` AND `oneToOne`.`field` = 'value'
LEFT OUTER JOIN (`AssocModel` AS `models2->AssocModel` INNER JOIN `Model2` AS `models2`
ON `models2`.`id` = `models2->AssocModel`.`model2Id`)
ON `Model1`.`id` = `models2->AssocModel`.`model2Id`;And it naturally fails with
message: You have an error in your SQL syntax; check the manual that corresponds to your MySQL
server version for the right syntax to use near '*)) AS `count` FROM `Model1` AS `Model1` INNER
JOIN `OneToOneModel` AS `oneToOne`' at line 1, stack: SequelizeDatabaseError: You have an error in
your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax
to use near '*)) AS `count` FROM `Model1` AS `Model1` INNER JOIN `OneToOneModel` AS
`oneToOne`' at line 1
So, in fact, we have two issues here:
- distinct option does not work as expected
- findAndCountAll does not work as expected with n:m assocciation
Dialect: mysql
Dialect version: 5
Sequelize version: 4.38.0
Reacted by Ivan Drenjanin, Dakota Hipp and Janvario
Activity
stale commentedon Oct 14, 2018
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂
NaifousBS commentedon Oct 24, 2018
+1
JamesMGreene commentedon Mar 15, 2019
Give this workaround a try: