Problems sorting by a relationship field #3978
Replies: 4 comments 16 replies
-
Hi @dkanthofer - currently this is the expected behavior. We would like to accomodate sorting on relationship fields in the future, which will require building some new functionality - so I'm going to convert this to a feature request and we'll get it on our roadmap.
EDIT: The workaround above is outdated and no longer recommended, it is likely to cause further issues and complications in your project. |
Beta Was this translation helpful? Give feedback.
-
hi guys is it still the current behavior ? |
Beta Was this translation helpful? Give feedback.
-
Is this supported in Payload 3? |
Beta Was this translation helpful? Give feedback.
-
Hi all – thanks for all the input here and sorry for the delayed response on this thread. To clarify:
We agree this behavior should be accommodated - ideally by sorting on Currently our recommended workaround is to use virtual fields as outlined in this recent feature. For example, if I have a collections: [
{
slug: 'posts',
fields: [
{
name: 'title',
type: 'text',
},
{
name: 'author',
type: 'relationship',
relationTo: 'author',
},
// To sort by the author name, instead of the ID, assign it to a virtual field
// This will create a new column in the collection view that can be used to sort
{
name: 'authorName',
virtual: 'author.name',
type: 'text',
},
],
},
{
slug: 'author',
fields: [
{
name: 'name',
type: 'text',
},
],
},
], This virtual field will be displayed in the collection list view and can be used to sort as expected. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Link to reproduction
N/A
Describe the Bug
When sorting a list by a relationship field, it actually sorts by the related document's ID. It should be alphabetically sorted by the field defined in
useAsTitle
of the related collection.As you can see in the screenshot, the Testrelation column seems to be sorted randomly, while in fact this is the alphabetical order of the IDs from the related collection.
To Reproduce
relationExamples
collection, add at least a title field and define it as the useAsTitle:Payload Version
2.0.13
Adapters and Plugins
db-mongodb, bundler-webpack
Beta Was this translation helpful? Give feedback.
All reactions