docs: add warning about unique fields with drafts enabled#16096
docs: add warning about unique fields with drafts enabled#16096efe-arv wants to merge 2 commits intopayloadcms:mainfrom
Conversation
Document the known limitation where `unique: true` fields on collections with `versions.drafts` enabled cause silent document creation failures. The Admin UI redirects to `?notFound=<id>` with no error message. This has been an open issue since 2022 (payloadcms#953, payloadcms#12628, payloadcms#3576) affecting users on both MongoDB and PostgreSQL/Drizzle adapters. Added: - Warning banner in Database changes section - New 'Unique fields with Drafts' section with: - Problem explanation - Application-level uniqueness workaround using validate + index - Code example Relates to payloadcms#953, payloadcms#12628, payloadcms#3576
235e068 to
2c7e905
Compare
jhb-dev
left a comment
There was a problem hiding this comment.
I think this should just be a count operation or at least set depth to 0 and select to {}, to prevent fetching and populating data that is not needed here.
|
Good call — updated to use |
|
Hi @jhb-dev — just a gentle ping. The |
|
Hi @efe-arv, nice you added the perf improvement! Unfortunately I am not a maintainer of this project, so you have to wait for a review of a team member. |
What
Adds documentation about a known limitation where
unique: truefields on collections withversions.draftsenabled cause silent document creation failures.The Problem
When a drafts-enabled collection has fields with
unique: true, creating new documents can fail silently:_collection_v)UNIQUEconstraint conflicts during the draft creation process?notFound=<id>with no visible error messageThis affects all database adapters (PostgreSQL/Drizzle and MongoDB) and has been reported multiple times:
What This PR Adds
unique: truewithindex: true+ application-levelvalidatefunctionThe Workaround
Context
We hit this in production on a legal CMS (Payload 3.64.0 + PostgreSQL on Fly.io) with 5,000+ tags. The collection had
versions.draftsenabled withunique: trueon bothnameandslugfields. Tag creation was completely broken with no error feedback — just a?notFound=redirect. Replacing the DB-level unique constraint with an application-level validate function resolved it immediately.Ideally Payload would handle this gracefully at the framework level (e.g., deferring unique validation for draft documents, or surfacing a clear error), but until then, this documentation helps others avoid the same trap.