Suggestion: Unique should allow empty values #542
-
I noticed that if Unique is enabled, multiple blank values are disallowed. This essentially means Unique implies Required. I think it should work like this: Required: no blanks allowed In my use case, I want each record in my collection to be optionally associated with one unique uid. Is there a way to express this type of constraint? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
This is a little tricky because we don't store A somewhat hacky workaround would be to create a partial unique index via sql for your column manually (or via migration): app.Dao().DB().NewQuery(`
CREATE UNIQUE INDEX your_unique_index_name ON YOUR_COLLECTION_NAME (uid) WHERE uid != '';
`).Execute(); |
Beta Was this translation helpful? Give feedback.
I'm not sure I follow. I thought this was already resolved with the recent Indexes UI.
Note that PocketBase doesn't store
null
values (with exception of thejson
type field). If you want the unique constraint to ignore empty values use a partial index as shown above, aka. specifying a WHERE clause that will skip the empty values from the unique check. The index can be specified from the Admin UI > Edit collections > "Unique constraints and indexes" section.