Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Versions - make sure parent key is the same type as parent _id. #6352

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

apetrisor
Copy link

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:

Screenshot 2024-05-14 at 13 42 57

Then, when building version collection fields, we can pass in the id type of the parent collection:

Screenshot 2024-05-14 at 13 43 33

Finally, when we build our schema we try for one of the allowed id types, falling back to the default Schema.Types.Mixed.

Screenshot 2024-05-14 at 13 44 29
Screenshot 2024-05-14 at 13 45 19

Other ways to solve this would be quite hacky and prone to bugs down the line:

  • In the relationship field schema generator, check whether this is the parent key of a version collection, then look at the parent collection and figure out the id type. - Quite bad
  • In db-mongodb's init function, when we generate the versionSchema, check the parent collection and override the version's schema as needed. - Better but not really

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.

  • I have read and understand the CONTRIBUTING.md document in this repository.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • This change requires a documentation update

Checklist:

  • Existing test suite passes locally with my changes

@apetrisor
Copy link
Author

I will investigate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant