fix: Versions - make sure parent key is the same type as parent _id. #6352
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Fix for issue #6349
When creating versions in mongodb, the type of the parent key needs to match the type of the _id of the parent. Otherwise you might end up with strings instead of numbers which will break the UI (seeing multiple versions when you shouldn't, not seeing the latest version, etc).
There are a few ways to solve this, however the most elegant I've found was to add support for an optional property to relationship fields, where we can manually specify the type of the parent key. I called this idType:
Then, when building version collection fields, we can pass in the id type of the parent collection:
Finally, when we build our schema we try for one of the allowed id types, falling back to the default Schema.Types.Mixed.
Other ways to solve this would be quite hacky and prone to bugs down the line:
The solution proposed uses payload's own API, it is clear and easy to understand, and opens the possibility of adding your own custom IDs in relationship fields - completely optional with no side effects.
Have not tested this bug on Postgres, however if this PR is accepted, it should be fairly simple for someone to use the idType property with similar logic in db-postgres.
I can make the required changes to the documentation if this is accepted.
Type of change
Checklist: