@@ -856,6 +856,75 @@ describe('database', () => {
856
856
}
857
857
} )
858
858
859
+ it ( 'should populate distinct relationships of hasMany: true when depth>0' , async ( ) => {
860
+ await payload . delete ( { collection : 'posts' , where : { } } )
861
+ await payload . delete ( { collection : 'categories' , where : { } } )
862
+
863
+ const categories = [ 'category-1' , 'category-2' , 'category-3' , 'category-4' ] . map ( ( title ) => ( {
864
+ title,
865
+ } ) )
866
+
867
+ const categoriesIDS : { categories : string } [ ] = [ ]
868
+
869
+ for ( const { title } of categories ) {
870
+ const doc = await payload . create ( { collection : 'categories' , data : { title } } )
871
+ categoriesIDS . push ( { categories : doc . id } )
872
+ }
873
+
874
+ await payload . create ( {
875
+ collection : 'posts' ,
876
+ data : {
877
+ title : '1' ,
878
+ categories : [ categoriesIDS [ 0 ] ?. categories , categoriesIDS [ 1 ] ?. categories ] ,
879
+ } ,
880
+ } )
881
+
882
+ await payload . create ( {
883
+ collection : 'posts' ,
884
+ data : {
885
+ title : '2' ,
886
+ categories : [
887
+ categoriesIDS [ 0 ] ?. categories ,
888
+ categoriesIDS [ 2 ] ?. categories ,
889
+ categoriesIDS [ 3 ] ?. categories ,
890
+ ] ,
891
+ } ,
892
+ } )
893
+
894
+ await payload . create ( {
895
+ collection : 'posts' ,
896
+ data : {
897
+ title : '3' ,
898
+ categories : [
899
+ categoriesIDS [ 0 ] ?. categories ,
900
+ categoriesIDS [ 3 ] ?. categories ,
901
+ categoriesIDS [ 1 ] ?. categories ,
902
+ ] ,
903
+ } ,
904
+ } )
905
+
906
+ const resultDepth0 = await payload . findDistinct ( {
907
+ collection : 'posts' ,
908
+ sort : 'categories.title' ,
909
+ field : 'categories' ,
910
+ } )
911
+ expect ( resultDepth0 . values ) . toStrictEqual ( categoriesIDS )
912
+ const resultDepth1 = await payload . findDistinct ( {
913
+ depth : 1 ,
914
+ collection : 'posts' ,
915
+ field : 'categories' ,
916
+ sort : 'categories.title' ,
917
+ } )
918
+
919
+ for ( let i = 0 ; i < resultDepth1 . values . length ; i ++ ) {
920
+ const fromRes = resultDepth1 . values [ i ] as any
921
+ const id = categoriesIDS [ i ] . categories as any
922
+ const title = categories [ i ] ?. title
923
+ expect ( fromRes . categories . title ) . toBe ( title )
924
+ expect ( fromRes . categories . id ) . toBe ( id )
925
+ }
926
+ } )
927
+
859
928
describe ( 'Compound Indexes' , ( ) => {
860
929
beforeEach ( async ( ) => {
861
930
await payload . delete ( { collection : 'compound-indexes' , where : { } } )
0 commit comments