-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Field "unique: true" is not respected #11379
Comments
Please add a reproduction in order for us to be able to investigate. Depending on the quality of reproduction steps, this issue may be closed if no reproduction is provided. Why was this issue marked with the
|
Here is a repo with a recreation of this problem: https://github.com/oysteinwangen/payload-unique-bug To recreate this issue with non-unique slugs:
(If you create your first post with unique set to true from the beginning, then this isn't an issue). Another issue about the same thing: Validation for scheduled publishing.The unique validation isn't checked before pressing save or publish. This can cause problems when scheduling the publishing, as the user won't get a warning about invalid fields when clicking schedule... |
Hey @oysteinwangen - thanks for bringing this up! Something to note about indexes in MongoDB is that once an index is created on a field, it won’t automatically change if the field’s configuration is updated. So if you add To fix this, you can drop the existing index for that field and restart Payload—this will recreate the index as unique. If you’re working with a large database and need to avoid dropping indexes in production, you can use a migration to apply the index instead using the mongoose model call syncIndexes() with For additional context: the unique constraint is enforced at the database level. When a field is updated to include Regarding your note about validation for scheduled publishing—thanks for pointing that out as well! Since that’s a separate issue, please open a new issue for it so we can track it properly. I’ll be closing this thread since the indexing behavior is expected due to the reasons mentioned above. Thanks again for taking the time to report this! |
Describe the Bug
Even though I add "unique: true" for a slug field, it is still possible to create two documents in the same collection with the same slug.
Link to the code that reproduces this issue
npx create-payload-app@latest -t website
Reproduction Steps
In /src/fields/slug/index.ts
Add unique: true under slugField.
Use mongodb.
Try to create two categories or posts with the same slug.
Which area(s) are affected? (Select all that apply)
area: core
Environment Info
The text was updated successfully, but these errors were encountered: