Data integrity and type safety when introducing new fields #12614
Unanswered
PatrickBauer
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hey everyone 👋
Just getting started with Payload and liking it quite a bit. However, there was a small hiccup in my starting hours.
I wanted to start a new project with Payload CMS and am currently getting familiar with the system. I come with a fairly extensive background in various CMS platforms, so I’d like to check in advance for some pitfalls I’ve encountered in other projects.
When I worked with the test installation and created a few test posts in the admin interface, I was able to query all this data. The generated TypeScript type also contains all fields as I would expect.
If I now add new fields that are required and also give these fields a default value, everything works fine. When fetching the data from the database, everything checks out and the default value is returned.
However, there is a problem with the TypeScript types for the posts that already exist, because if I don't provide a default value for the new field, the TypeScript types are still specified as if the fields were available when querying this data, while at runtime, after these fields have been fetched from the database, they are not defined, which of course leads to errors.
Unfortunately, I couldn't find anything about this in the documentation, and of course I am aware of the option to write migration scripts that retroactively update content, but still, I wanted to know if there is a best practice for this, or if I am simply using the system incorrectly 🙏
Beta Was this translation helpful? Give feedback.
All reactions