Skip to content

Missing documentation for unique: boolean select prop #12836

@todor0v

Description

@todor0v

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

Metadata

Metadata

Assignees

Labels

area: docsImprovements or additions to documentation

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions