我是Postgresql的新手,无法重构某些由我编写的代码。代码审阅者不接受我的解决方案,以为没有子查询就可以完成。
static async deleteAllByDomains(clientId, domains = []) {
await SegmentDomain.query()
.select()
.innerJoin(function () {
this.select(['id'])
.from('segments as s')
.where({client_id: clientId})
.andWhere({deleted_at: null})
.as('s');
}, 's.id', 'segments-domains.segment_id')
.whereIn('domain', domains)
.del();
}
我有2张桌子**segments** and **segments-domains** .
This is the relation between them in Segments model
static get relationMappings() {
return {
domains: {
relation: Model.HasManyRelation,
modelClass: SegmentDomain,
join: {
from: R.concat(R.__, '.segment_id')(SegmentDomain.tableName),
to: R.concat(R.__, '.id')(Segment.tableName),
},
},
campaign_ids: {
relation: Model.HasManyRelation,
modelClass: SegmentCampaign,
join: {
from: R.concat(R.__, '.segment_id')(SegmentCampaign.tableName),
to: R.concat(R.__, '.id')(Segment.tableName),
},
},
};
}
段表结构`
段域表结构`
目标是按域名从segments-domains中删除行,并且仅删除segment id与segments-domains segment_id相匹配的行。我还需要通过client_id和Deleted_at从细分表中进行过滤。 可以在没有innerJoin的情况下完成,或者如果我使用innerJoin,那么在没有子查询的情况下可以做到吗?