@@ -1310,6 +1310,29 @@ describe('database', () => {
13101310 ] )
13111311 } )
13121312
1313+ it ( 'should find distinct values when the virtual field is linked to ID' , async ( ) => {
1314+ await payload . delete ( { collection : 'posts' , where : { } } )
1315+ await payload . delete ( { collection : 'categories' , where : { } } )
1316+ const category = await payload . create ( {
1317+ collection : 'categories' ,
1318+ data : { title : 'category' } ,
1319+ } )
1320+ await payload . create ( { collection : 'posts' , data : { title : 'post' , category } } )
1321+ const distinct = await payload . findDistinct ( { collection : 'posts' , field : 'categoryID' } )
1322+ expect ( distinct . values ) . toStrictEqual ( [ { categoryID : category . id } ] )
1323+ } )
1324+
1325+ it ( 'should find distinct values by the explicit ID field path' , async ( ) => {
1326+ await payload . delete ( { collection : 'posts' , where : { } } )
1327+ await payload . delete ( { collection : 'categories' , where : { } } )
1328+ const category = await payload . create ( {
1329+ collection : 'categories' ,
1330+ data : { title : 'category' } ,
1331+ } )
1332+ await payload . create ( { collection : 'posts' , data : { title : 'post' , category } } )
1333+ const distinct = await payload . findDistinct ( { collection : 'posts' , field : 'category.id' } )
1334+ expect ( distinct . values ) . toStrictEqual ( [ { 'category.id' : category . id } ] )
1335+ } )
13131336 describe ( 'Compound Indexes' , ( ) => {
13141337 beforeEach ( async ( ) => {
13151338 await payload . delete ( { collection : 'compound-indexes' , where : { } } )
0 commit comments