【问题标题】:Duplicate rows using left join at Sequelize在 Sequelize 中使用左连接复制行
【发布时间】:2019-12-09 20:16:22
【问题描述】:

我在 Sequelize postgress by limit 和 offset 使用 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);})

【问题讨论】:

  • 你是如何设置 startRowIndex 的?此外,当您使用limit/offset 时,您应该使用order 来强制执行正确的分页。

标签: node.js postgresql orm sequelize.js


【解决方案1】:

在您的查询中使用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);})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-17
    • 2013-03-03
    • 2013-02-13
    • 2015-01-12
    相关资源
    最近更新 更多