What happens
When I mark a select sub-field inside an array as unique: true, Payload creates a global, non-sparse multi-key index on that path. As a result, any second document—even one without that array—fails with:
The following field is invalid: "blocks.myBlock.field"
Expected
- Uniqueness enforced only within each
items array in myBlock.
- Other blocks or documents without
myBlock are unaffected.
Actual
- A global unique index spans all documents in the collection.
- Documents lacking the array still collide on a
null entry for that field.
Missing Documentation
I am not able to find sufficient documentation for the unique: boolean property of a field of type: 'select'. What I was able to find is the following sentence for some other field types such as text, textarea, email:
Enforce that each entry in the Collection has a unique value for this field.
However this is not sufficient in my case.
Reproduction
Let's say I have a block called "myBlock" with the following array field in it:
{
name: 'items',
type: 'array',
fields: [
{
name: 'key',
type: 'select',
required: true,
unique: true,
options: [
{ label: 'name', value: 'name' },
{ label: 'type', value: 'type' },
],
},
],
},
The key has the unique property. I would assume this would mean that all the items in the array (for myBlock) can be used once and that other blocks would not be affected. However, what is happening is that every document in a collection, even one without a myBlock in it, would have to be unique.
For example I get an error in my production environment of:
The following field is invalid: "blocks.myBlock.key".
This is happening when creating another collection entry that uses a totally different block that has no such field inside it. This behavior is not described anywhere that I was able to find.
Additional Details
Payload CMS version: 3.41.0
MongoDB version: 6.17.0
What happens
When I mark a
selectsub-field inside an array asunique: true, Payload creates a global, non-sparse multi-key index on that path. As a result, any second document—even one without that array—fails with:Expected
itemsarray inmyBlock.myBlockare unaffected.Actual
nullentry for that field.Missing Documentation
I am not able to find sufficient documentation for the
unique: booleanproperty of a field oftype: 'select'. What I was able to find is the following sentence for some other field types such as text, textarea, email:However this is not sufficient in my case.
Reproduction
Let's say I have a block called "myBlock" with the following array field in it:
The
keyhas theuniqueproperty. I would assume this would mean that all the items in the array (formyBlock) can be used once and that other blocks would not be affected. However, what is happening is that every document in a collection, even one without amyBlockin it, would have to be unique.For example I get an error in my production environment of:
This is happening when creating another collection entry that uses a totally different block that has no such field inside it. This behavior is not described anywhere that I was able to find.
Additional Details
Payload CMS version:
3.41.0MongoDB version:
6.17.0