在Sequelize中使用左连接重复行

时间:2019-12-09 07:59:45

标签: node.js postgresql orm sequelize.js

我使用LEFT JOIN来按限制和偏移量对后继进行排序。   当我更改下一页的限制和偏移量时,最后一页的末行重复  在下一页的第一行。

关系:

 service.hasMany(servicePicture, { foreignKey: 'ServiceID', targetKey: 'ServiceID' });
 servicePicture.belongsTo(service, { foreignKey: 'ServiceID', targetKey: 'ServiceID' });

我正在使用:

models.Service.findAll({
attributes: attributes,
where: whereClause,

include:     {

    model: models.ServicePicture ,
    attributes:[
        'ServicePictureID',
        'ServicePicturePath'  
    ],
    where: {IsDeleted : false},
    required:false,

   } ,

subQuery:false,
offset: startRowIndex, 
limit: recordCount,

 })
 .then(serviceModels =>{
resolve(serviceModels);})

1 个答案:

答案 0 :(得分:0)

在查询中使用distinct:true

models.Service.findAll({
attributes: attributes,
where: whereClause,

include:     {

    model: models.ServicePicture ,
    attributes:[
        'ServicePictureID',
        'ServicePicturePath'  
    ],
    where: {IsDeleted : false},
    required:false,

   } ,

subQuery:false,
offset: startRowIndex, 
limit: recordCount,
distinct:true // add this distinct here

 })
 .then(serviceModels =>{
resolve(serviceModels);})