mirror of
https://github.com/strapi/strapi.git
synced 2025-08-07 08:16:35 +00:00
fix manyWay deep filtering when the relation is self
Signed-off-by: Pierre Noël <pierre.noel@strapi.io>
This commit is contained in:
parent
8aa50cd80c
commit
5ffe33c530
@ -99,18 +99,20 @@ const buildJoinsAndFilter = (qb, model, whereClauses) => {
|
||||
);
|
||||
} else if (assoc.nature === 'manyWay') {
|
||||
const joinTableAlias = generateAlias(assoc.tableCollectionName);
|
||||
const isRelatedToSameTable =
|
||||
destinationInfo.model.collectionName === originInfo.model.collectionName;
|
||||
|
||||
qb.leftJoin(
|
||||
`${originInfo.model.databaseName}.${assoc.tableCollectionName} AS ${joinTableAlias}`,
|
||||
`${joinTableAlias}.${singular(originInfo.alias)}_id`,
|
||||
`${joinTableAlias}.${singular(originInfo.model.collectionName)}_id`,
|
||||
`${originInfo.alias}.${originInfo.model.primaryKey}`
|
||||
);
|
||||
|
||||
qb.leftJoin(
|
||||
`${destinationInfo.model.databaseName}.${destinationInfo.model.collectionName} AS ${destinationInfo.alias}`,
|
||||
`${joinTableAlias}.${singular(originInfo.model.attributes[assoc.alias].attribute)}_${
|
||||
originInfo.model.attributes[assoc.alias].column
|
||||
}`,
|
||||
`${joinTableAlias}.${isRelatedToSameTable ? 'related_' : ''}${singular(
|
||||
originInfo.model.attributes[assoc.alias].attribute
|
||||
)}_${originInfo.model.attributes[assoc.alias].column}`,
|
||||
`${destinationInfo.alias}.${destinationInfo.model.primaryKey}`
|
||||
);
|
||||
} else {
|
||||
|
@ -33,6 +33,11 @@ const collector = {
|
||||
target: 'application::panini-card.panini-card',
|
||||
unique: false,
|
||||
},
|
||||
collector_friends: {
|
||||
nature: 'manyWay',
|
||||
target: '__self__',
|
||||
unique: false,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
@ -72,6 +77,7 @@ async function createFixtures() {
|
||||
body: {
|
||||
name: 'Isabelle',
|
||||
panini_cards: [data.paniniCards[0].id],
|
||||
collector_friends: [data.collectors[0].id],
|
||||
},
|
||||
});
|
||||
data.collectors.push(collector2Res.body);
|
||||
@ -137,4 +143,22 @@ describe('Deep Filtering API', () => {
|
||||
expect(res.body[0]).toMatchObject(data.collectors[0]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Filter on a self manyWay relation', () => {
|
||||
test('Should return 1 result', async () => {
|
||||
const res = await rq({
|
||||
method: 'GET',
|
||||
url: '/collectors',
|
||||
qs: {
|
||||
'collector_friends.name': data.collectors[0].name,
|
||||
},
|
||||
});
|
||||
|
||||
console.log('res', JSON.stringify(res.body, null, 2));
|
||||
|
||||
expect(Array.isArray(res.body)).toBe(true);
|
||||
expect(res.body.length).toBe(1);
|
||||
expect(res.body[0]).toMatchObject(data.collectors[1]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user